This commit is contained in:
Victor_Wang 2024-08-01 14:06:23 +08:00
parent cc8e50108b
commit 8c3c8dd433
16 changed files with 348 additions and 1 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9d618dd32a7c5c1489edab87f23d7e92
guid: ae49ca4dac5aa2b4dba32bbcd0f0723a
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dfc97fe817fcd3a459c9283999f9a6a0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,37 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 19101, guid: 0000000000000000e000000000000000, type: 0}
m_Name: PanelSettings
m_EditorClassIdentifier:
themeUss: {fileID: -4733365628477956816, guid: 967f2d6279a3ca9478e9b28ab8de8eb8, type: 3}
m_TargetTexture: {fileID: 0}
m_ScaleMode: 1
m_Scale: 1
m_ReferenceDpi: 96
m_FallbackDpi: 96
m_ReferenceResolution: {x: 1200, y: 800}
m_ScreenMatchMode: 0
m_Match: 0
m_SortingOrder: 0
m_TargetDisplay: 0
m_ClearDepthStencil: 1
m_ClearColor: 0
m_ColorClearValue: {r: 0, g: 0, b: 0, a: 0}
m_DynamicAtlasSettings:
m_MinAtlasSize: 64
m_MaxAtlasSize: 4096
m_MaxSubTextureSize: 64
m_ActiveFilters: 31
m_AtlasBlitShader: {fileID: 9101, guid: 0000000000000000f000000000000000, type: 0}
m_RuntimeShader: {fileID: 9100, guid: 0000000000000000f000000000000000, type: 0}
m_RuntimeWorldShader: {fileID: 9102, guid: 0000000000000000f000000000000000, type: 0}
textSettings: {fileID: 0}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2d91bcfcb89e2734cae6453f561ba863
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 87f0785827ff4524799000723ebb1de6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1 @@
@import url("unity-theme://default");

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 967f2d6279a3ca9478e9b28ab8de8eb8
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12388, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f364c5dfb64761a44b0e3c1d9c09b85f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d9bddd5bb91de864cb92e214f08e49f7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,205 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using DG.Tweening;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using System.Reflection;
using System;
using System.Linq;
public class AutoGenerate : EditorWindow
{
private VisualElement rootElement;
private Button generateBtn;
private Label hintText;
private TextField nameInput;
private string scenePath;
private string scriptPath;
private string prefabPath;
[MenuItem("Window/Generate Editor Window")]
public static void ShowExample()
{
AutoGenerate wnd = GetWindow<AutoGenerate>();
wnd.titleContent = new GUIContent("Auto Generate UI Window");
}
public void CreateGUI()
{
// scenePath = Application.dataPath + "/TestUIPanelScenes";
// scriptPath = Application.dataPath + "/Scripts/Project/UI/UI_Panel";
// prefabPath = Application.dataPath + "/Resources/UI/UI_Panel";
scenePath = "";
scriptPath = "Assets/Test2";
prefabPath = "Assets/Resources/UI/UI_Panel";
// scenePath = "Assets/TestUIPanelScenes";
// scriptPath = "Assets/Scripts/Project/UI/UI_Panel";
// prefabPath = "Assets/Resources/UI/UI_Panel";
// string xmlPath = Path.Combine(Application.dataPath, "ThridPart", "UIToolKitFile", "UDoc", "UXml", "AutoGenerateUI.uxml");
// string xmlPath = "Assets/UIToolKitFile/UDoc/UXml/AutoGenerateUI.uxml";
var visualTree =
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/ThirdPart/UIToolKitFile/UDoc/UXml/AutoGenerateUI.uxml");
rootElement = visualTree.CloneTree();
rootVisualElement.Add(rootElement);
generateBtn = rootElement.Q<Button>("AutoGenerate");
hintText = rootElement.Q<Label>("HintTitle");
nameInput = rootElement.Q<TextField>("NameInput");
generateBtn.clicked += () => { OnGenerateButtonClick(); };
}
private void OnGenerateButtonClick()
{
GameObject selectedObject = Selection.activeGameObject;
if (selectedObject == null)
{
ShowHint("未选择物体,未添加任何文件");
return;
}
string tempName = selectedObject.name;
DirectoryDontExist(prefabPath);
DirectoryDontExist(scenePath);
DirectoryDontExist(scriptPath);
var sceneExist = DoesFileExist(scenePath, tempName);
var preExist = DoesFileExist(prefabPath, tempName);
var scriptExist = DoesFileExist(scriptPath, tempName);
if (sceneExist || preExist || scriptExist)
{
ShowHint("三者中已经有存在的文件,未添加任何文件");
return;
}
string scriptContent = GetScriptTemplate(tempName);
File.WriteAllText(Path.Combine(scriptPath, tempName + ".cs"), scriptContent);
var generatePre =
PrefabUtility.SaveAsPrefabAsset(selectedObject, Path.Combine(prefabPath, tempName + ".prefab"));
CreateNewScene(scenePath,tempName);
AssetDatabase.Refresh();
//EditorUtility.DisplayDialog("成功创建", $" " + scenePath, "OK");
}
private void DirectoryDontExist(string directoryPath)
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
private string GetScriptTemplate(string scriptName)
{
return $@"
using UnityEngine;
public class {scriptName} : MonoBehaviour
{{
void Start()
{{
}}
}}";
}
/// <summary>
/// 提示文本
/// </summary>
/// <param name="mes"></param>
private void ShowHint(string mes)
{
hintText.text = mes;
hintText.style.opacity = 1f; // 确保提示文本完全可见
DOVirtual.DelayedCall(2, () => { hintText.text = string.Empty; });
}
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public bool DoesFileExist(string path, string fileName)
{
string fullPath = path;
string[] files = Directory.GetFiles(fullPath, "*.*", SearchOption.TopDirectoryOnly); //不递归
foreach (var file in files)
{
if (Path.GetFileNameWithoutExtension(file) == fileName)
{
return true;
}
}
return false;
}
/// <summary>
/// 创建场景
/// </summary>
/// <param name="scenePath"></param>
/// <param name="sceneName"></param>
private void CreateNewScene(string scenePath, string sceneName)
{
var newScene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
// GameObject lightGameObject = new GameObject("Directional Light");
// Light lightComp = lightGameObject.AddComponent<Light>();
// lightComp.type = LightType.Directional;
// lightComp.transform.rotation = Quaternion.Euler(50, -30, 0);
//
// GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
// ground.transform.position = Vector3.zero;
// 加载预制体资源
GameObject prefab =
AssetDatabase.LoadAssetAtPath<GameObject>(Path.Combine("Assets", "Resources", "UI", "Base",
"Canvas.prefab"));
if (prefab != null)
{
// 实例化预制体并添加到场景中
GameObject prefabInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
SceneManager.MoveGameObjectToScene(prefabInstance, newScene);
}
else
{
Debug.LogError("预制体未找到,请检查路径是否正确!");
}
EditorSceneManager.SaveScene(newScene, scenePath+"/"+sceneName+".unity",true);
//EditorUtility.DisplayDialog("Scene Created", "New scene has been created and saved to " + scenePath, "OK");
}
private void AttachScriptToGameObject(string className)
{
// 获取当前的Assembly
var assembly = Assembly.Load("Assembly-CSharp");
// 使用反射获取新脚本的类型
Type scriptType = assembly.GetTypes().FirstOrDefault(t => t.Name == className);
if (scriptType != null)
{
// 查找目标游戏对象(假设场景中有一个名为 "TargetObject" 的对象)
GameObject targetObject = GameObject.Find("TargetObject");
if (targetObject != null)
{
// 添加脚本到对象上
targetObject.AddComponent(scriptType);
Debug.Log($"{className} script attached to {targetObject.name}");
}
else
{
Debug.LogError("Target object not found in the scene.");
}
}
else
{
Debug.LogError($"Script type {className} not found.");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dc9a6dbf66a3cfa42b3dcc614a54adcb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 295a26bcb8a694c448afdec8cd4e8540
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f5cbd23a5e6726149b01f994ae90be70
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f092cea7379703f449e5862d165db263
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:VisualElement style="height: 451px;">
<ui:Label text="自动生成脚本,预制体,测试场景" display-tooltip-when-elided="true" name="Title" style="margin-top: 40px; margin-right: auto; margin-bottom: auto; margin-left: auto; transform-origin: right; font-size: 30px; -unity-text-align: middle-left;" />
<ui:Label display-tooltip-when-elided="true" name="HintTitle" style="margin-top: 40px; margin-right: auto; margin-bottom: auto; margin-left: auto; transform-origin: right; font-size: 50px; -unity-text-align: middle-left;" />
<ui:Button text="AutoGenerate&#10;" display-tooltip-when-elided="true" name="AutoGenerate" style="margin-top: auto; margin-right: auto; margin-bottom: auto; margin-left: auto; font-size: 30px; height: 70px;" />
<ui:TextField picking-mode="Ignore" label="暂时没用" value="filler text" name="NameInput" style="margin-top: 28px; margin-right: auto; margin-bottom: auto; margin-left: auto; -unity-text-align: middle-center; font-size: 20px;" />
</ui:VisualElement>
</ui:UXML>

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 3b676839f162eba43b48b486a92b26ff
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}