Exemplo n.º 1
0
        public static void CreatePefabParts(GameObject go, ref int indenitity, List <PrefabPartInfo> prefabParts, int parentId = -1, int depth = 0)
        {
            Component[] components = go.GetComponents <Component>();
            foreach (Component component in components)
            {
                PrefabPartInfo componentPart = new PrefabPartInfo();
                componentPart.ParentPersistentID = parentId;
                componentPart.PersistentID       = indenitity;
                componentPart.Object             = component;
                componentPart.Depth = depth;
                indenitity++;
                prefabParts.Add(componentPart);
            }

            if (go.transform.childCount > 0)
            {
                foreach (Transform child in go.transform)
                {
                    PrefabPartInfo childPart = new PrefabPartInfo();
                    childPart.ParentPersistentID = parentId;
                    childPart.PersistentID       = indenitity;
                    childPart.Object             = child.gameObject;
                    childPart.Depth = depth;
                    indenitity++;
                    prefabParts.Add(childPart);
                    CreatePefabParts(child.gameObject, ref indenitity, prefabParts, childPart.PersistentID, depth + 1);
                }
            }
        }
Exemplo n.º 2
0
        private void Sync(
            AssetLibraryInfo assetLibraryInfo,
            int parentPersistentID,
            int depth,
            GameObject go,
            Dictionary <UnityObject, PrefabPartInfo> objToParts,
            List <PrefabPartInfo> newPrefabs)
        {
            Component[] components = go.GetComponents <Component>();
            for (int i = 0; i < components.Length; ++i)
            {
                Component component = components[i];
                if (component == null)
                {
                    continue;
                }
                PrefabPartInfo part;
                if (objToParts.TryGetValue(component, out part))
                {
                    newPrefabs.Add(part);
                }
                else
                {
                    part = new PrefabPartInfo
                    {
                        Depth              = depth,
                        Object             = component,
                        PersistentID       = assetLibraryInfo.Identity,
                        ParentPersistentID = parentPersistentID
                    };
                    assetLibraryInfo.Identity++;
                    newPrefabs.Add(part);
                }
            }

            foreach (Transform child in go.transform)
            {
                GameObject     childGo = child.gameObject;
                PrefabPartInfo part;
                if (objToParts.TryGetValue(childGo, out part))
                {
                    newPrefabs.Add(part);
                }
                else
                {
                    part = new PrefabPartInfo
                    {
                        Depth              = depth,
                        Object             = childGo,
                        PersistentID       = assetLibraryInfo.Identity,
                        ParentPersistentID = parentPersistentID
                    };
                    assetLibraryInfo.Identity++;
                    newPrefabs.Add(part);
                }

                Sync(assetLibraryInfo, part.PersistentID, depth + 1, childGo, objToParts, newPrefabs);
            }
        }
Exemplo n.º 3
0
        private void LoadIDMappingTo(AssetInfo asset,
                                     MappingInfo mapping,
                                     List <int> instanceIDs,
                                     List <int> persistentIDs,
                                     bool IIDtoPID, bool PIDtoObj)
        {
            if (IIDtoPID)
            {
                int instanceID = asset.Object.GetInstanceID();

                //Following if statement required because object can be added to multiple libraries and
                //have multiple persistetnt identifiers. Only first persistent identifier is added to dictionary.
                //Any persistent id can be converted to "first" persistent id using following approach:
                //(PersistenID -> Obj) -> (InstanceID -> First PersistentID)
                if (!mapping.InstanceIDtoPID.ContainsKey(instanceID))
                {
                    mapping.Add(instanceID, m_offset + asset.PersistentID);
                    instanceIDs.Add(instanceID);
                }
            }

            if (PIDtoObj)
            {
                int persistentID = m_offset + asset.PersistentID;
                mapping.Add(persistentID, asset.Object);
                persistentIDs.Add(persistentID);
            }

            if (asset.PrefabParts != null)
            {
                for (int i = 0; i < asset.PrefabParts.Count; ++i)
                {
                    PrefabPartInfo prefabPart = asset.PrefabParts[i];
                    if (prefabPart != null && prefabPart.Object != null)
                    {
                        if (IIDtoPID)
                        {
                            int instanceID = prefabPart.Object.GetInstanceID();
                            if (!mapping.InstanceIDtoPID.ContainsKey(instanceID))
                            {
                                mapping.Add(instanceID, m_offset + prefabPart.PersistentID);
                                instanceIDs.Add(instanceID);
                            }
                        }

                        if (PIDtoObj)
                        {
                            int persistentID = m_offset + prefabPart.PersistentID;
                            mapping.Add(persistentID, prefabPart.Object);
                            persistentIDs.Add(persistentID);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static void CreatePefabParts(GameObject go, ref int identity, Dictionary <UnityObject, int> objToId, List <PrefabPartInfo> prefabParts, int parentId = -1, int depth = 0)
        {
            Component[] components = go.GetComponents <Component>();
            foreach (Component component in components)
            {
                PrefabPartInfo componentPart = new PrefabPartInfo();
                componentPart.ParentPersistentID = parentId;
                if (objToId.ContainsKey(component))
                {
                    componentPart.PersistentID = objToId[component];
                }
                else
                {
                    componentPart.PersistentID = identity;
                    identity++;
                }

                componentPart.Object = component;
                componentPart.Depth  = depth;
                prefabParts.Add(componentPart);
            }

            if (go.transform.childCount > 0)
            {
                foreach (Transform child in go.transform)
                {
                    PrefabPartInfo childPart = new PrefabPartInfo();
                    childPart.ParentPersistentID = parentId;
                    if (objToId.ContainsKey(child.gameObject))
                    {
                        childPart.PersistentID = objToId[child.gameObject];
                    }
                    else
                    {
                        childPart.PersistentID = identity;
                        identity++;
                    }

                    childPart.Object = child.gameObject;
                    childPart.Depth  = depth;
                    prefabParts.Add(childPart);
                    CreatePefabParts(child.gameObject, ref identity, objToId, prefabParts, childPart.PersistentID, depth + 1);
                }
            }
        }
Exemplo n.º 5
0
        public bool IsSyncRequired()
        {
            if (Object == null)
            {
                return(false);
            }

            GameObject go = Object as GameObject;

            if (go == null)
            {
                return(false);
            }

            PrefabPartInfo rootPart = new PrefabPartInfo
            {
                Object             = go,
                Depth              = -1,
                ParentPersistentID = -1,
                PersistentID       = -1,
            };

            Dictionary <UnityObject, PrefabPartInfo> objToParts = PrefabParts != null?
                                                                  PrefabParts.Where(part => part.Object != null).ToDictionary(part => part.Object) :
                                                                      new Dictionary <UnityObject, PrefabPartInfo>();

            objToParts.Add(go, rootPart);

            Dictionary <int, PrefabPartInfo> idToParts = PrefabParts != null?
                                                         PrefabParts.Where(part => part.Object != null).ToDictionary(part => part.PersistentID) :
                                                             new Dictionary <int, PrefabPartInfo>();

            idToParts.Add(rootPart.PersistentID, rootPart);

            return(IsSyncRequired(go, objToParts, idToParts));
        }
Exemplo n.º 6
0
        private static void CreateAssetLibrary(object[] objects, string folderName, string assetLibraryName, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            int identity = asset.AssetLibrary.Identity;

            foreach (object o in objects)
            {
                UnityObject obj = o as UnityObject;
                if (!obj)
                {
                    if (o != null)
                    {
                        Debug.Log(o.GetType() + " is not a UnityEngine.Object");
                    }
                    continue;
                }

                if (hs.Contains(obj))
                {
                    continue;
                }

                if (!AssetDatabase.Contains(obj))
                {
                    continue;
                }

                if (obj is GameObject)
                {
                    GameObject go        = (GameObject)obj;
                    AssetInfo  assetInfo = new AssetInfo(go.name, 0, identity);
                    assetInfo.Object = go;
                    hs.Add(go);

                    identity++;

                    List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                    AssetLibraryAssetsGUI.CreatePefabParts(go, ref identity, prefabParts);
                    for (int i = prefabParts.Count - 1; i >= 0; --i)
                    {
                        PrefabPartInfo prefabPart = prefabParts[i];
                        if (hs.Contains(prefabPart.Object))
                        {
                            prefabParts.Remove(prefabPart);
                        }
                        else
                        {
                            hs.Add(prefabPart.Object);
                        }
                    }

                    if (prefabParts.Count >= AssetLibraryInfo.MAX_ASSETS - AssetLibraryInfo.INITIAL_ID)
                    {
                        EditorUtility.DisplayDialog("Unable Create AssetLibrary", string.Format("Max 'Indentity' value reached. 'Identity' ==  {0}", AssetLibraryInfo.MAX_ASSETS), "OK");
                        return;
                    }

                    if (identity >= AssetLibraryInfo.MAX_ASSETS)
                    {
                        SaveAssetLibrary(asset, folderName, assetLibraryName, index);
                        index++;

                        asset  = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                        folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();
                        if (folder.Assets == null)
                        {
                            folder.Assets = new List <AssetInfo>();
                        }
                        identity = asset.AssetLibrary.Identity;
                    }

                    assetInfo.PrefabParts       = prefabParts;
                    asset.AssetLibrary.Identity = identity;
                    folder.Assets.Add(assetInfo);
                    assetInfo.Folder = folder;
                }
                else
                {
                    AssetInfo assetInfo = new AssetInfo(obj.name, 0, identity);
                    assetInfo.Object = obj;
                    identity++;

                    if (identity >= AssetLibraryInfo.MAX_ASSETS)
                    {
                        SaveAssetLibrary(asset, folderName, assetLibraryName, index);
                        index++;

                        asset  = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                        folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();
                        if (folder.Assets == null)
                        {
                            folder.Assets = new List <AssetInfo>();
                        }
                        identity = asset.AssetLibrary.Identity;
                    }

                    asset.AssetLibrary.Identity = identity;
                    folder.Assets.Add(assetInfo);
                    assetInfo.Folder = folder;
                }
            }

            SaveAssetLibrary(asset, folderName, assetLibraryName, index);
            index++;

            //Selection.activeObject = asset;
            //EditorGUIUtility.PingObject(asset);
        }
Exemplo n.º 7
0
        private IList <AssetInfo> GetData()
        {
            if (m_folders != null)
            {
                List <AssetInfo> result = new List <AssetInfo>();

                AssetInfo root = new AssetInfo
                {
                    id        = 0,
                    name      = "Root",
                    IsEnabled = false,
                    depth     = -1,
                };

                result.Add(root);
                foreach (AssetInfo assetInfo in m_folders.Where(folder => folder.Assets != null).SelectMany(folder => folder.Assets))
                {
                    assetInfo.parent = root;
                    result.Add(assetInfo);

                    if (assetInfo.PrefabParts != null)
                    {
                        Dictionary <int, AssetInfo> assets = new Dictionary <int, AssetInfo>();
                        assets.Add(-1, assetInfo);

                        for (int i = 0; i < assetInfo.PrefabParts.Count; ++i)
                        {
                            PrefabPartInfo prefabPart = assetInfo.PrefabParts[i];
                            string         name;
                            if (prefabPart.Object == null)
                            {
                                name = "<Null>";
                            }
                            else
                            {
                                if (prefabPart.Object is Component)
                                {
                                    name = prefabPart.Object.GetType().Name;
                                }
                                else
                                {
                                    name = prefabPart.Object.name;
                                }
                            }

                            AssetInfo prefabPartAssetInfo = new AssetInfo(name, assetInfo.depth + prefabPart.Depth + 1, prefabPart.PersistentID);
                            prefabPartAssetInfo.Object = prefabPart.Object;

                            assets.Add(prefabPartAssetInfo.id, prefabPartAssetInfo);

                            if (assets.ContainsKey(prefabPart.ParentPersistentID))
                            {
                                TreeElement parent = assets[prefabPart.ParentPersistentID];
                                prefabPartAssetInfo.parent = parent;
                                result.Add(prefabPartAssetInfo);
                            }
                        }
                    }
                }
                return(result);
            }

            return(new List <AssetInfo>
            {
                new AssetInfo
                {
                    id = 0,
                    name = "Root",
                    IsEnabled = false,
                    depth = -1,
                }
            });
        }
Exemplo n.º 8
0
        private bool CreateAsset(UnityObject obj, AssetInfo parentAssetInfo, int insertIndex = -1, AssetFolderInfo folder = null)
        {
            if (obj is GameObject)
            {
                GameObject go = (GameObject)obj;

                int identity = m_asset.AssetLibrary.Identity + 1;
                List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                CreatePefabParts(go, ref identity, prefabParts);
                if (identity >= AssetLibraryInfo.MAX_ASSETS)
                {
                    return(false);
                }

                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);

                assetInfo.PrefabParts         = prefabParts;
                m_asset.AssetLibrary.Identity = identity;

                if (folder != null)
                {
                    if (IsFolderSelected(folder))
                    {
                        if (assetInfo.PrefabParts != null)
                        {
                            Dictionary <int, AssetInfo> assets = new Dictionary <int, AssetInfo>();
                            assets.Add(-1, assetInfo);

                            for (int i = 0; i < assetInfo.PrefabParts.Count; ++i)
                            {
                                PrefabPartInfo prefabPart = assetInfo.PrefabParts[i];
                                string         name;
                                if (prefabPart.Object == null)
                                {
                                    name = "<Null>";
                                }
                                else
                                {
                                    if (prefabPart.Object is Component)
                                    {
                                        name = prefabPart.Object.GetType().Name;
                                    }
                                    else
                                    {
                                        name = prefabPart.Object.name;
                                    }
                                }

                                AssetInfo prefabPartAssetInfo = new AssetInfo(name, assetInfo.depth + prefabPart.Depth, prefabPart.PersistentID);
                                prefabPartAssetInfo.Object = prefabPart.Object;

                                assets.Add(prefabPartAssetInfo.id, prefabPartAssetInfo);

                                TreeElement parent = assets[prefabPart.ParentPersistentID];
                                TreeView.treeModel.AddElement(prefabPartAssetInfo, parent, parent.children != null ? parent.children.Count : 0);
                            }
                        }
                    }
                }
            }
            else
            {
                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);
            }
            return(true);
        }
        private bool CreateAsset(UnityObject obj, AssetInfo parentAssetInfo, int insertIndex = -1, AssetFolderInfo folder = null)
        {
            if (obj is GameObject)
            {
                GameObject go = (GameObject)obj;

                int identity = m_asset.AssetLibrary.Identity + 1;
                List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                CreatePefabParts(go, ref identity, prefabParts);


                string prefabPath = AssetDatabase.GetAssetPath(go);
                if (!string.IsNullOrEmpty(prefabPath))
                {
                    UnityObject[] assetRepresentations = AssetDatabase.LoadAllAssetRepresentationsAtPath(prefabPath);
                    foreach (UnityObject assetRepresentation in assetRepresentations)
                    {
                        //Add avatar or mesh as prefab part
                        if (assetRepresentation is Avatar || assetRepresentation is Mesh)
                        {
                            PrefabPartInfo prefabPart = new PrefabPartInfo();
                            prefabPart.ParentPersistentID = -1;
                            prefabPart.PersistentID       = identity;
                            prefabPart.Object             = assetRepresentation;
                            prefabPart.Depth = 0;
                            identity++;
                            prefabParts.Add(prefabPart);
                        }
                    }
                }

                if (identity >= AssetLibraryInfo.MAX_ASSETS)
                {
                    return(false);
                }

                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);

                assetInfo.PrefabParts         = prefabParts;
                m_asset.AssetLibrary.Identity = identity;

                if (folder != null)
                {
                    if (IsFolderSelected(folder))
                    {
                        if (assetInfo.PrefabParts != null)
                        {
                            Dictionary <int, AssetInfo> assets = new Dictionary <int, AssetInfo>();
                            assets.Add(-1, assetInfo);

                            for (int i = 0; i < assetInfo.PrefabParts.Count; ++i)
                            {
                                PrefabPartInfo prefabPart = assetInfo.PrefabParts[i];
                                string         name;
                                if (prefabPart.Object == null)
                                {
                                    name = "<Null>";
                                }
                                else
                                {
                                    if (prefabPart.Object is Component)
                                    {
                                        name = prefabPart.Object.GetType().Name;
                                    }
                                    else
                                    {
                                        name = prefabPart.Object.name;
                                    }
                                }

                                AssetInfo prefabPartAssetInfo = new AssetInfo(name, assetInfo.depth + prefabPart.Depth, prefabPart.PersistentID);
                                prefabPartAssetInfo.Object = prefabPart.Object;

                                assets.Add(prefabPartAssetInfo.id, prefabPartAssetInfo);

                                TreeElement parent = assets[prefabPart.ParentPersistentID];
                                TreeView.treeModel.AddElement(prefabPartAssetInfo, parent, parent.children != null ? parent.children.Count : 0);
                            }
                        }
                    }
                }
            }
            else
            {
                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);
            }
            return(true);
        }