UItoolkit

This commit is contained in:
Victor_Wang 2024-08-02 08:43:22 +08:00
parent fc5ba37ae1
commit ac98963e63
9 changed files with 305 additions and 53 deletions

View File

@ -1,5 +1,6 @@
fileFormatVersion: 2
guid: 771f1f1e73ccf2941b2a201a0aa65f57
guid: facce128213d0e0428f2ac35d1af2d0a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:

8
Assets/Project/UI.meta Normal file
View File

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

View File

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

View File

@ -37,6 +37,7 @@ RectTransform:
- {fileID: 8048042365574858743}
- {fileID: 8048042366265002465}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -62,7 +63,6 @@ Canvas:
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
@ -135,6 +135,7 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8048042365323826983}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -170,6 +171,7 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8048042365323826983}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -205,6 +207,7 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8048042365323826983}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -236,13 +239,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8048042366265002466}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -405, y: -331.5, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8048042365323826983}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8048042366265002479
MonoBehaviour:
@ -306,11 +309,115 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Children:
- {fileID: 5727611685167956682}
m_Father: {fileID: 8048042365323826983}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1001 &1053102348014623454
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 8048042366342728953}
m_Modifications:
- target: {fileID: 901144683796615499, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_Name
value: KLKLU
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_Pivot.x
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_Pivot.y
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_RootOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchorMax.x
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchorMax.y
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchorMin.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchoredPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
--- !u!224 &5727611685167956682 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 4747303608412444692, guid: c0203dda7880fc04987c6b9f478a6e97, type: 3}
m_PrefabInstance: {fileID: 1053102348014623454}
m_PrefabAsset: {fileID: 0}

View File

@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1672172331512302196
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6509094311855932203}
- component: {fileID: 705147604200775653}
- component: {fileID: 8333258407669994010}
m_Layer: 5
m_Name: CustomPanel
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6509094311855932203
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1672172331512302196}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &705147604200775653
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1672172331512302196}
m_CullTransparentMesh: 1
--- !u!114 &8333258407669994010
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1672172331512302196}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 0.392}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 6057d8d0491426b4292c05a6a698d1ee
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,10 +4,10 @@ using UnityEngine;
public class #SCRIPTNAME# : BasePanel
{
protected override void Awake()
{
base.Awake();
}
public void Init()
{
}
void Start()
{
@ -20,17 +20,17 @@ public class #SCRIPTNAME# : BasePanel
public override void ShowMe()
{
base.ShowMe();
}
public override void HideMe()
{
base.HideMe();
}
protected override void OnClick(string btnName)
{
base.OnClick(btnName);
switch (btnName)
{
case "":
@ -40,18 +40,5 @@ public class #SCRIPTNAME# : BasePanel
}
}
protected override void OnChangeToggle(string toogleName, bool isOn)
{
base.OnChangeToggle(toogleName, isOn);
}
protected override void OnChangeSlider(string SliderName, float value)
{
base.OnChangeSlider(SliderName, value);
}
protected override void OnChangeInputFile(string inputFileName, string value)
{
base.OnChangeInputFile(inputFileName, value);
}
}

View File

@ -20,7 +20,7 @@ public class AutoGenerate : EditorWindow
private string scenePath;
private string scriptPath;
private string prefabPath;
[MenuItem("Window/Generate Editor Window")]
public static void ShowExample()
{
@ -30,20 +30,17 @@ public class AutoGenerate : EditorWindow
public void CreateGUI()
{
// scenePath = Application.dataPath + "/TestUIPanelScenes";
// scriptPath = Application.dataPath + "/Scripts/Project/UI/UI_Panel";
// prefabPath = Application.dataPath + "/Resources/UI/UI_Panel";
scenePath = "";
// scenePath = "Assets/SandBox/TestUIPanelScenes";
// scriptPath = "Assets/Project/UI/UI_Panel";
// prefabPath = "Assets/Resources/UI/UI_Panel";
scenePath = "Assets/Test1";
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";
prefabPath = "Assets/Test3";
var visualTree =
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/ThirdPart/UIToolKitFile/UDoc/UXml/AutoGenerateUI.uxml");
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
"Assets/ThirdPart/UIToolKitFile/UDoc/UXml/AutoGenerateUI.uxml");
rootElement = visualTree.CloneTree();
rootVisualElement.Add(rootElement);
generateBtn = rootElement.Q<Button>("AutoGenerate");
@ -54,14 +51,28 @@ public class AutoGenerate : EditorWindow
private void OnGenerateButtonClick()
{
GameObject selectedObject = Selection.activeGameObject;
string tempName = nameInput.text;
//Selection.activeGameObject;
GameObject selectedObjectRes = AssetDatabase.LoadAssetAtPath<GameObject>(Path.Combine("Assets", "SandBox",
"TestUIPanelScenes", "OnlyUITemplate",
"CustomPanel.prefab"));
GameObject selectedObject = (GameObject)PrefabUtility.InstantiatePrefab(selectedObjectRes);
PrefabUtility.UnpackPrefabInstance(selectedObject, PrefabUnpackMode.Completely,
InteractionMode.AutomatedAction);
// selectedObject.transform.localPosition=Vector3.zero;
// selectedObject.transform.localScale=Vector3.one;
selectedObject.name = tempName;
if (selectedObject == null)
{
ShowHint("未选择物体,未添加任何文件");
ShowHint("预制体不存在,未添加任何文件");
return;
}
string tempName = selectedObject.name;
DirectoryDontExist(prefabPath);
DirectoryDontExist(scenePath);
DirectoryDontExist(scriptPath);
@ -75,12 +86,28 @@ public class AutoGenerate : EditorWindow
return;
}
//
string scriptContent = GetScriptTemplate(tempName);
File.WriteAllText(Path.Combine(scriptPath, tempName + ".cs"), scriptContent);
AssetDatabase.Refresh();
var generatePre =
PrefabUtility.SaveAsPrefabAsset(selectedObject, Path.Combine(prefabPath, tempName + ".prefab"));
CreateNewScene(scenePath,tempName);
CreateNewScene(scenePath, tempName);
// TODO 绑定脚本的功能没实现AttachScriptToGameObject(tempName, generatePre);
// PrefabUtility.SavePrefabAsset(generatePre);
AssetDatabase.Refresh();
// EditorApplication.delayCall = () =>
// {
// AssetDatabase.Refresh();
// AttachScriptToGameObject(tempName, generatePre);
// PrefabUtility.SavePrefabAsset(generatePre);
// AssetDatabase.Refresh();
// // EditorUtility.DisplayDialog("成功", "UI预制体,测试场景,UI脚本已经被创建 ", "OK");
// };
//AssetDatabase.Refresh();
//EditorUtility.DisplayDialog("成功创建", $" " + scenePath, "OK");
}
@ -97,12 +124,36 @@ public class AutoGenerate : EditorWindow
return $@"
using UnityEngine;
public class {scriptName} : MonoBehaviour
public class {scriptName} : BasePanel
{{
public void Init()
{{
}}
void Start()
{{
}}
public override void ShowMe()
{{
}}
public override void HideMe()
{{
}}
protected override void OnClick(string btnName)
{{
switch (btnName)
{{
case """":
break;
default:
break;
}}
}}
}}";
}
@ -154,27 +205,32 @@ public class {scriptName} : MonoBehaviour
// GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
// ground.transform.position = Vector3.zero;
// 加载预制体资源
// 加载预制体资源///
GameObject prefab =
AssetDatabase.LoadAssetAtPath<GameObject>(Path.Combine("Assets", "Resources", "UI", "Base",
"Canvas.prefab"));
GameObject pre = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath + "/sceneName.prefab");
if (prefab != null)
{
// 实例化预制体并添加到场景中
GameObject prefabInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
GameObject preInstance = (GameObject)PrefabUtility.InstantiatePrefab(pre);
SceneManager.MoveGameObjectToScene(prefabInstance, newScene);
SceneManager.MoveGameObjectToScene(preInstance, newScene);
preInstance.transform.parent = prefabInstance.transform.Find("Mid");
((RectTransform)preInstance.transform).localPosition = Vector3.zero;
((RectTransform)preInstance.transform).transform.localScale = Vector3.one;
}
else
{
Debug.LogError("预制体未找到,请检查路径是否正确!");
}
EditorSceneManager.SaveScene(newScene, scenePath+"/"+sceneName+".unity",true);
//EditorUtility.DisplayDialog("Scene Created", "New scene has been created and saved to " + scenePath, "OK");
EditorSceneManager.SaveScene(newScene, scenePath + "/" + sceneName + ".unity", true);
}
private void AttachScriptToGameObject(string className)
private void AttachScriptToGameObject(string className, GameObject pre)
{
// 获取当前的Assembly
var assembly = Assembly.Load("Assembly-CSharp");
@ -185,12 +241,12 @@ public class {scriptName} : MonoBehaviour
if (scriptType != null)
{
// 查找目标游戏对象(假设场景中有一个名为 "TargetObject" 的对象)
GameObject targetObject = GameObject.Find("TargetObject");
if (targetObject != null)
if (pre != null)
{
// 添加脚本到对象上
targetObject.AddComponent(scriptType);
Debug.Log($"{className} script attached to {targetObject.name}");
pre.AddComponent(scriptType);
ShowHint($"{className} script attached to {pre.name}");
}
else
{

View File

@ -3,6 +3,6 @@
<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:TextField picking-mode="Ignore" label="UI面板名" 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>