三国卡牌客户端基础资源仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.IO;
using System.Text;
 
public class PSDTOUGUIProcessor
{
    [UnityEditor.MenuItem("Assets/PSDTOUGUI后处理")]
    public static void BaseSettings()
    {   
        GameObject go = Selection.activeGameObject;
        if (go == null)
        {
            return;
        }
 
        // go.AddMissingComponent<Canvas>();
        // go.AddMissingComponent<Canvas>();
        // go.AddMissingComponent<Canvas>();
    }
 
    [UnityEditor.MenuItem("Assets/新UI处理")]
    public static void NewUIHandle()
    {
        GameObject go = Selection.activeGameObject;
        if (go == null)
        {
            return;
        }
 
        if (!go.name.EndsWith("Win"))
        {
            Debug.LogError("请选择正确的UI UI应该以Win结尾");
            return;
        }
 
        go.AddMissingComponent<Canvas>();
        go.AddMissingComponent<CanvasGroup>();
        go.AddMissingComponent<CanvasScaler>();
 
        var graphics = go.GetComponentsInChildren<Graphic>(true);
 
        foreach (Graphic g in graphics)
        {
            g.raycastTarget = false;
        }
    }
    
    [UnityEditor.MenuItem("GameObject/生成UI脚本", false, 10)]
    public static void GenerateUIScript()
    {
        GameObject go = Selection.activeGameObject;
        if (go == null)
        {
            Debug.LogError("请先选择一个GameObject");
            return;
        }
        
        string className = go.name;
        string targetFolder = Path.Combine(Application.dataPath, "Scripts/Main/UI");
        
        // 确保目标文件夹存在
        if (!Directory.Exists(targetFolder))
        {
            Directory.CreateDirectory(targetFolder);
        }
        
        string filePath = Path.Combine(targetFolder, className + ".cs");
        
        // 检查文件是否已存在
        if (File.Exists(filePath))
        {
            if (!EditorUtility.DisplayDialog("文件已存在", $"文件 {className}.cs 已存在,是否覆盖?", "覆盖", "取消"))
            {
                return;
            }
        }
        
        // 生成脚本内容
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("using UnityEngine;");
        sb.AppendLine("using System.Collections;");
        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine("using UnityEngine.UI;");
        sb.AppendLine("using DG.Tweening;");
        sb.AppendLine("using Cysharp.Threading.Tasks;");
        sb.AppendLine("");
        sb.AppendLine($"public class {className} : UIBase");
        sb.AppendLine("{");
        sb.AppendLine("    // 组件引用");
        sb.AppendLine("");
        sb.AppendLine("    // 生命周期");
        sb.AppendLine("    protected override void Awake()");
        sb.AppendLine("    {");
        sb.AppendLine("        base.Awake();");
        sb.AppendLine("        // 初始化组件引用");
        sb.AppendLine("    }");
        sb.AppendLine("");
        sb.AppendLine("    protected override void Start()");
        sb.AppendLine("    {");
        sb.AppendLine("        base.Start();");
        sb.AppendLine("        // 初始化数据");
        sb.AppendLine("    }");
        sb.AppendLine("");
        sb.AppendLine("    // UI事件");
        sb.AppendLine("    protected override void OnOpen()");
        sb.AppendLine("    {");
        sb.AppendLine("        base.OnOpen();");
        sb.AppendLine("        // 窗口打开时的逻辑");
        sb.AppendLine("    }");
        sb.AppendLine("");
        sb.AppendLine("    protected override void OnClose()");
        sb.AppendLine("    {");
        sb.AppendLine("        base.OnClose();");
        sb.AppendLine("        // 窗口关闭时的逻辑");
        sb.AppendLine("    }");
        sb.AppendLine("");
        sb.AppendLine("    public override void Refresh()");
        sb.AppendLine("    {");
        sb.AppendLine("        base.Refresh();");
        sb.AppendLine("        // 刷新UI显示");
        sb.AppendLine("    }");
        sb.AppendLine("}");
        
        // 写入文件
        File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8);
        
        Debug.Log($"UI脚本已生成: {filePath}");
        
        // 刷新资源数据库以显示新文件
        AssetDatabase.Refresh();
        
        // 打开生成的脚本
        UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(filePath, 1);
 
    }
}