public GameObject replace(GameObject fbxPrefab, GameObject fbxRawModel)
        {
            GameObject prefabInstance = GameObject.Instantiate(fbxPrefab);

            prefabInstance.name = fbxPrefab.name;
            prefabInstance.SetActive(false);
            prefabInstance.hideFlags = HideFlags.HideAndDontSave;

            PathComponentRecord fbxRecord = this.recordListFromHierarchy(prefabInstance, fbxDictionary);

            var path = AssetDatabase.GetAssetPath(fbxPrefab);

            fbxPrefab = PrefabUtility.SaveAsPrefabAssetAndConnect(fbxRawModel, path, InteractionMode.AutomatedAction);
            PathComponentRecord modelRecord = this.recordListFromHierarchy(fbxPrefab, modelDictionary);

            ///先添加组件及基本信息
            doReplace(fbxRecord, modelRecord, false);
            ///绑定引用类型
            doReplace(fbxRecord, modelRecord, true);

            GameObject.DestroyImmediate(prefabInstance);

            /*SpringManager sm= fbxPrefab.GetComponent<SpringManager>();
             * if (sm != null)
             * {
             *  sm.OnValidate();
             * }*/

            return(fbxPrefab);
        }
Example #2
0
        public GameObject replace(GameObject fbxPrefab, GameObject fbxRawModel)
        {
            PathComponentRecord prefabHierarchyRecord = recordListFromHierarchy(fbxPrefab, fbxDictionary);
            PathComponentRecord modelHierarchyRecord  = recordListFromHierarchy(fbxRawModel, modelDictionary);

            bindComponentHierarchy(prefabHierarchyRecord, modelHierarchyRecord);

            if (isDirty)
            {
                SkinnedMeshRenderer[] prefabSkinnedMeshRenderers =
                    fbxPrefab.GetComponentsInChildren <SkinnedMeshRenderer>();
                SkinnedMeshRenderer[] modelSkinnedMeshRenderers =
                    fbxRawModel.GetComponentsInChildren <SkinnedMeshRenderer>();
                foreach (SkinnedMeshRenderer modelSkinnedMeshRenderer in modelSkinnedMeshRenderers)
                {
                    foreach (SkinnedMeshRenderer prefabSkinnedMeshRenderer in prefabSkinnedMeshRenderers)
                    {
                        if (prefabSkinnedMeshRenderer.name == modelSkinnedMeshRenderer.name)
                        {
                            bindBone(prefabSkinnedMeshRenderer, modelSkinnedMeshRenderer);
                        }
                    }
                }
            }

            return(fbxPrefab);
        }
        public void createChild(PathComponentRecord modelChild)
        {
            GameObject gameObject = GameObject.Instantiate(modelChild.go, go.transform, false);

            gameObject.name = modelChild.go.name;
            Transform newTransform = gameObject.transform;

            newTransform.localPosition = modelChild.localPosition;
            newTransform.localRotation = modelChild.localRotation;
            newTransform.localScale    = modelChild.localScale;
        }
Example #4
0
        protected virtual PathComponentRecord getNewByOld(Transform oldTransform)
        {
            PathComponentRecord t = null;
            PathComponentRecord f = fbxMap.get(oldTransform);

            if (f != null)
            {
                t = modelMap.get(f.path);
            }
            return(t);
        }
        private void doReplace(PathComponentRecord fbxRecord, PathComponentRecord modelRecord, bool isResolve)
        {
            foreach (ComponentRecord fbxComponent in fbxRecord.components)
            {
                Type type = fbxComponent.type;
                if (type == null)
                {
                    continue;
                }

                Component nc = modelRecord.go.GetComponent(type);
                if (nc == null)
                {
                    nc = modelRecord.go.AddComponent(type);
                }

                if (nc != null)
                {
                    if (isResolve)
                    {
                        IPrefabCopyResolve resolver;
                        if (Resolves.TryGetValue(nc.GetType(), out resolver))
                        {
                            resolver.init(fbxDictionary, modelDictionary);
                            resolver.resolve(nc, fbxComponent);
                        }
                    }
                    else
                    {
                        EditorJsonUtility.FromJsonOverwrite(fbxComponent.json, nc);
                    }
                }
            }

            foreach (PathComponentRecord fbxRecordChild in fbxRecord.children)
            {
                foreach (PathComponentRecord modelRecordChild in modelRecord.children)
                {
                    if (modelRecordChild.name == fbxRecordChild.name)
                    {
                        doReplace(fbxRecordChild, modelRecordChild, isResolve);
                        break;
                    }
                }
            }
        }
        private PathComponentRecord recordListFromHierarchy(GameObject gameObject, RecordMaping recordMaping, string parentPath = "")
        {
            PathComponentRecord result = new PathComponentRecord();

            List <ComponentRecord> hierarchy = new List <ComponentRecord>();

            foreach (Component component in gameObject.GetComponents <Component>())
            {
                if (component is Transform || component is Renderer)
                {
                    continue;
                }

                ComponentRecord record = new ComponentRecord();
                record.type      = component.GetType();
                record.component = component;
                record.json      = EditorJsonUtility.ToJson(component);
                hierarchy.Add(record);
            }

            result.rootMap    = recordMaping;
            result.name       = gameObject.name;
            result.path       = parentPath + "/" + result.name;
            result.go         = gameObject;
            result.components = hierarchy;

            result.localPosition = gameObject.transform.localPosition;
            result.localRotation = gameObject.transform.localRotation;
            result.localScale    = gameObject.transform.localScale;

            recordMaping.add(result);

            int childCount = gameObject.transform.childCount;

            for (int i = 0; i < childCount; i++)
            {
                PathComponentRecord item =
                    this.recordListFromHierarchy(gameObject.transform.GetChild(i).gameObject, recordMaping, result.path);

                result.children.Add(item);
            }

            return(result);
        }
Example #7
0
        private void bindComponentHierarchy(PathComponentRecord prefab, PathComponentRecord model)
        {
            bool has = false;

            foreach (PathComponentRecord modelChild in model.children)
            {
                has = false;
                foreach (PathComponentRecord prefabChild in prefab.children)
                {
                    if (prefabChild.name == modelChild.name)
                    {
                        has = true;
                        bindComponentHierarchy(prefabChild, modelChild);
                        break;
                    }
                }

                if (has == false)
                {
                    isDirty = true;
                    prefab.createChild(modelChild);
                }
            }
        }
 public void add(PathComponentRecord record)
 {
     transMap.Add(record.go.transform, record);
     pathMap.Add(record.path, record);
 }