示例#1
0
    static void AddScript()
    {
        if (EditorApplication.isCompiling || EditorApplication.isUpdating)
        {
            EditorUtility.DisplayDialog("info", "稍后再尝试.... Unity正在编译", "OK");
            return;
        }

        string[]            assetGUIDs = Selection.assetGUIDs;
        UGUIScriptGenerator temp       = new UGUIScriptGenerator();

        for (int i = 0; i < assetGUIDs.Length; i++)
        {
            string path  = GetPathRelativeToResourceByGUID(assetGUIDs[i]);
            int    index = path.LastIndexOf("/");
            string name  = path.Substring(path.LastIndexOf("/") + 1);

            bool find = false;
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type t = assembly.GetType(string.Format("{0}.{1}", UIGlobalDefine.UINameSPace, name + UIGlobalDefine.ViewScriptSuffix)); //version44.2
                if (t != null)
                {
                    //添加对应的Script脚本 到prefab上
                    UnityEngine.Object obj = Resources.Load(path);
                    GameObject         go  = GameObject.Instantiate(obj) as GameObject;

                    UIViewBase test = go.GetComponent(t) as UIViewBase;
                    if (test == null)
                    {
                        test = go.AddComponent(t) as UIViewBase;
                        test.Awake();
                        //EditorUtility.SetDirty(go);
                        PrefabUtility.ReplacePrefab(go, obj);
                    }

                    GameObject.DestroyImmediate(go);
                    obj  = null;
                    go   = null;
                    find = true;
                    break;
                }
            }

            if (!find)
            {
                Debug.LogWarning("没找到prefab对应的脚本:" + path);
            }
        }

        Resources.UnloadUnusedAssets();
    }