示例#1
0
        public static void generateScriptForGraphic(MenuCommand command)
        {
            Graphic graphic = command.context as Graphic;

            if (graphic.material == null)
            {
                Debug.LogError(graphic + "没有材质!");
                return;
            }
            if (graphic.material.shader == null)
            {
                Debug.LogError(graphic + "的材质" + graphic.material + "没有着色器!");
                return;
            }
            string path = generateScript(graphic.material.shader);

            UncompiledComponent.add(graphic.gameObject, path);
        }
示例#2
0
        private static string generateOrUpdateRoot(string dir, GameObject parentGameObject, GameObject rootGameObject, Type baseType = null, string className = null)
        {
            if (updatedGameObjectDic.ContainsKey(rootGameObject))
            {
                //Debug.Log("忽略重复的GameObject" + rootGameObject.name, rootGameObject);
                return(updatedGameObjectDic[rootGameObject]);
            }
            GameObject prefabGameObject     = null;
            GameObject rootPrefabGameObject = null;
            string     prefabPath           = null;

            if (PrefabUtility.IsAnyPrefabInstanceRoot(rootGameObject))
            {
                prefabGameObject = PrefabUtility.GetCorrespondingObjectFromSource(rootGameObject);
                if (updatedGameObjectDic.ContainsKey(prefabGameObject))
                {
                    //Debug.Log("忽略重复的PrefabInstance" + rootGameObject, rootGameObject);
                    return(updatedGameObjectDic[prefabGameObject]);
                }
                prefabPath           = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefabGameObject);
                rootPrefabGameObject = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);
                if (updatedGameObjectDic.ContainsKey(rootPrefabGameObject))
                {
                    return(updatedGameObjectDic[rootPrefabGameObject]);
                }
                Debug.Log("更新或生成预制件" + rootGameObject.name + ",路径:" + prefabPath, rootGameObject);
                rootGameObject   = PrefabUtility.LoadPrefabContents(prefabPath);
                parentGameObject = null;//Prefab是没有Parent的。
            }
            //确定文件名
            Component component;

            if (baseType == null)
            {
                component = rootGameObject.GetComponent(typeof(UIObject));
                if (component != null)
                {
                    baseType = component.GetType().BaseType;
                }
                else
                {
                    baseType = typeof(UIObject);
                }
            }
            else
            {
                component = rootGameObject.GetComponent(baseType);
            }
            className = getFixedClassName(rootGameObject, baseType, className);
            string     targetPath = dir.Replace('\\', '/') + "/" + className + ".cs";
            MonoScript script;
            string     rPath;

            if (component != null)
            {
                script = MonoScript.FromMonoBehaviour(component as MonoBehaviour);
                rPath  = AssetDatabase.GetAssetPath(script);
                string originPath = Environment.CurrentDirectory.Replace('\\', '/') + "/" + rPath;
                if (targetPath != originPath)
                {
                    if (pref.dontAskRegenerate ? false : EditorUtility.DisplayDialog("UGUI AutoScript", rootGameObject.name + "已有的组件脚本位置与目标位置不一致,是否要在目标位置创建一个新的脚本文件?", "Yes", "No"))
                    {
                        UnityEngine.Object.DestroyImmediate(component);//删除已有组件
                        component = null;
                        script    = null;
                        rPath     = targetPath.Replace(Environment.CurrentDirectory.Replace('\\', '/') + "/", string.Empty);
                    }
                    else
                    {
                        targetPath = originPath;
                        className  = script.GetClass().Name;
                    }
                }
            }
            else
            {
                rPath  = targetPath.Replace(Environment.CurrentDirectory.Replace('\\', '/') + "/", string.Empty);
                script = AssetDatabase.LoadAssetAtPath <MonoScript>(rPath);
            }
            updatedGameObjectDic.Add(rootGameObject, className);
            if (prefabGameObject != null)
            {
                updatedGameObjectDic.Add(prefabGameObject, className);
            }
            if (rootPrefabGameObject != null && !updatedGameObjectDic.ContainsKey(rootPrefabGameObject))
            {
                updatedGameObjectDic.Add(rootPrefabGameObject, className);
            }
            //加载脚本文件
            FileInfo fileInfo   = new FileInfo(targetPath);
            Type     scriptType = script != null?script.GetClass() : null;

            //生成脚本文件
            CodeCompileUnit unit = generateCompileUnit(dir, parentGameObject, rootGameObject, baseType, className);

            CodeDOMHelper.writeUnitToFile(fileInfo, unit);
            if (component == null)
            {
                if (scriptType != null)
                {
                    component = rootGameObject.GetComponent(scriptType);
                    if (component == null)
                    {
                        component = rootGameObject.AddComponent(scriptType);
                    }
                    try
                    {
                        component.GetType().GetMethod("autoBind").Invoke(component, new object[] { });
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarning("自动绑定组件失败:" + e + "\n请尝试重新生成脚本", rootGameObject);
                    }
                }
                else
                {
                    UncompiledComponent addComponent = rootGameObject.AddComponent <UncompiledComponent>();
                    addComponent.path = rPath;
                    if (prefabGameObject != null)
                    {
                        pref.updateList.Add(prefabGameObject);
                    }
                }
            }
            else
            {
                try
                {
                    component.GetType().GetMethod("autoBind").Invoke(component, new object[] { });
                }
                catch (Exception e)
                {
                    Debug.LogWarning("自动绑定组件失败:" + e + "\n请尝试重新生成脚本", rootGameObject);
                }
            }
            if (prefabGameObject != null)
            {
                PrefabUtility.SaveAsPrefabAsset(rootGameObject, prefabPath);
                PrefabUtility.UnloadPrefabContents(rootGameObject);
            }
            return(className);
        }