示例#1
0
        //[MenuItem("Tools/Prefab Generate")]
        /// param: @Type, @Config {Name, Modellist}
        ///
        public static void PrefabGenerator(CharacterConfig config, string Type)
        {
            //if (Type == "Poster")
            TextAsset jsonStr = AssetDatabase.LoadAssetAtPath <TextAsset>("Assets/Text/Poster.json");
            RawData   data    = RawData.Load(jsonStr.text);

            var root = new GameObject("Root");

            //get prefab name
            root.name = config.GetPrefabName(config.Name, Type);

            foreach (RawComponent sub in data.subs)
            {
                switch (sub.Type)
                {
                case ComponentType.GameObject:
                    if (!string.IsNullOrEmpty(sub.SourceName))
                    {
                        var parent = GetOrCreateObject(root.transform, sub.Parent);
                        var source = config.GetModelPath(sub.SourceName);
                        // get origin model
                        GameObject modelRef = null;

                        // only for body
                        if (data.Readable)
                        {
                            var readableSource = source.Replace(".fbx", ".readable.fbx");
                            if (!File.Exists(string.Concat(modelPath, "/", readableSource)))
                            {
                                CreateReadableModelAsset(modelPath, source, readableSource);
                            }
                            modelRef = AssetDatabase.LoadAssetAtPath <GameObject>(string.Concat(modelPath, "/", readableSource));
                        }
                        else
                        {
                            modelRef = AssetDatabase.LoadAssetAtPath <GameObject>(string.Concat(modelPath, "/", source));
                        }

                        GameObject model = PrefabUtility.InstantiatePrefab(modelRef) as GameObject;
                        model.transform.SetParent(parent);

                        foreach (Transform ob in model.transform)
                        {
                            if (ob.name == "Root" || ob.name == sub.Name)
                            {
                                continue;
                            }
                            ob.gameObject.SetActive(false);
                        }
                        model.name = sub.Name;
                    }
                    else
                    {
                        var        em_parent = GetOrCreateObject(root.transform, sub.Parent);
                        GameObject empty     = new GameObject(sub.Name);
                        empty.transform.parent = em_parent;
                    }

                    break;

                case ComponentType.BoxCollider:
                    var ob_col = GetOrCreateObject(root.transform, sub.Parent);
                    var col    = ob_col.gameObject.AddComponent <BoxCollider>();
                    col.transform.localPosition = sub.Params.position;
                    break;

                case ComponentType.PlayableDirector:
                    var ob_tl = GetOrCreateObject(root.transform, sub.Parent);
                    var tl    = ob_tl.gameObject.AddComponent <PlayableDirector>();
                    tl.extrapolationMode = (DirectorWrapMode)sub.Params.directorWrapMode;
                    break;
                }
            }

            string genPrefabFullName = string.Concat(prefabDirectory, "/", root.name, prefabExtension);
            Object prefabObj         = PrefabUtility.SaveAsPrefabAsset(root, genPrefabFullName);


            GameObject.DestroyImmediate(root);
        }