Beispiel #1
0
 static Func <AssetBase, Boolean> ObjectFilter(String guid, Type type)
 {
     return(obj => obj && type.IsAssignableFrom(obj.GetType()) && obj.AssetObject != null && AssetObjectIdentifier.IsGuidValid(obj.AssetObject.Guid) && obj.AssetObject.Guid == guid);
 }
Beispiel #2
0
    static AssetBase[] LoadAll()
    {
        var all = UnityEngine.Resources.LoadAll <AssetBase>("DB").ToList();

        // load entity prefabs
        var prefabList = EntityPrefabsListAsset.Instance;

        if (prefabList && prefabList.Prefabs != null)
        {
            foreach (var prefabItem in prefabList.Prefabs)
            {
                if (prefabItem.Prefab)
                {
                    EntityPrefabAsset prefab;
                    prefab               = ScriptableObject.CreateInstance <EntityPrefabAsset>();
                    prefab.Settings      = new EntityPrefab();
                    prefab.Settings.Guid = prefabItem.Path;
                    prefab.Prefab        = prefabItem.Prefab;

                    all.Add(prefab);
                }
            }
        }

        // add default physics material
        {
            PhysicsMaterialAsset material;

            material               = ScriptableObject.CreateInstance <PhysicsMaterialAsset>();
            material.Settings      = SimulationConfigAsset.Instance.Configuration.Physics.DefaultPhysicsMaterial;
            material.Settings.Guid = Quantum.PhysicsMaterial.DEFAULT_ID;

            all.Add(material);
        }

        // add default physics material
        {
            NavMeshAgentConfigAsset agent;

            agent               = ScriptableObject.CreateInstance <NavMeshAgentConfigAsset>();
            agent.Settings      = SimulationConfigAsset.Instance.Configuration.NavMeshAgent.DefaultNavMeshAgent;
            agent.Settings.Guid = Quantum.NavMeshAgentConfig.DEFAULT_ID;

            all.Add(agent);
        }

        // call OnAssetLoad if callback exists
        // to allow user code to modify assets
        // before they are loaded into quantum
        if (OnAssetLoad != null)
        {
            OnAssetLoad(all);
        }

        // make sure we only have valid guids
        for (Int32 i = all.Count - 1; i >= 0; --i)
        {
            if (AssetObjectIdentifier.IsGuidValid(all[i].AssetObject.Guid) == false)
            {
                // log error
                Debug.LogErrorFormat("Asset '{0}' does not have a valid guid '{1}'. Asset guids have to be a non-zero length string which only contains ASCII characters. Asset '{0}' will not be loaded.", all[i].name, all[i].AssetObject.Guid);

                // remove this asset from the load table
                all.RemoveAt(i);
            }
        }

        // check for duplicate guids
        foreach (var group in all.GroupBy(x => x.AssetObject.Guid))
        {
            if (group.Count() > 1)
            {
                // log error
                Debug.LogErrorFormat("Assets '{0}' share the same guid '{1}', this is not allowed. Assets '{0}' will not be loaded.", String.Join("', '", group.Select(x => x.name).ToArray()), group.Key);

                // remove from list
                foreach (var asset in group)
                {
                    all.Remove(asset);
                }
            }
        }

        // sort based on guid
        all.Sort((a, b) => a.AssetObject.Guid.CompareTo(b.AssetObject.Guid));

        // create result
        var result = new AssetBase[all.Count + 1];

        // set ids (index + 1)
        for (Int32 i = 0; i < all.Count; ++i)
        {
            result[i + 1] = all[i];
            result[i + 1].AssetObject.Id = i + 1;
        }


        return(result);
    }
Beispiel #3
0
        public static void Assign()
        {
            // assign all missing id's
            foreach (var asset in UnityEngine.Resources.LoadAll <AssetBase>("DB"))
            {
                if (asset && asset.AssetObject != null)
                {
                    if (AssetObjectIdentifier.IsGuidValid(asset.AssetObject.Guid) == false)
                    {
                        asset.AssetObject.Guid = NewGuid();
                        EditorUtility.SetDirty(asset);
                    }
                }
            }

            // check for duplicates
            Dictionary <String, AssetBase> guids = new Dictionary <String, AssetBase>();

            foreach (var asset in UnityEngine.Resources.LoadAll <AssetBase>("DB"))
            {
                if (asset && asset.AssetObject != null)
                {
                    // check for duplicate
                    if (guids.ContainsKey(asset.AssetObject.Guid))
                    {
                        // log duplicate
                        Debug.LogFormat("Found duplicate GUID {0} on assets {1} and {2}, creating new guid for {2}", asset.AssetObject.Guid, guids[asset.AssetObject.Guid].name, asset.name);

                        // TODO: Here we should check which asset is the newest by inspecting the file system mtime
                        // and make sure that we change the guid of the asset which is newest.

                        // create new guid
                        do
                        {
                            asset.AssetObject.Guid = NewGuid();
                            EditorUtility.SetDirty(asset);
                        } while (guids.ContainsKey(asset.AssetObject.Guid));
                    }

                    guids.Add(asset.AssetObject.Guid, asset);
                }
            }

            var prefabList = EntityPrefabsListAsset.Instance;

            if (!prefabList)
            {
                AssetDatabase.CreateAsset(prefabList = ScriptableObject.CreateInstance <EntityPrefabsListAsset>(), "Assets/Quantum/Resources/EntityPrefabsList.asset");
            }

            prefabList.Prefabs = new EntityPrefabsListAsset.EntityPrefabItem[0];

            foreach (var prefab in UnityEngine.Resources.LoadAll <GameObject>("PREFABS"))
            {
                var root = prefab.GetComponent <EntityPrefabRoot>();
                if (root)
                {
                    var path = AssetDatabase.GetAssetPath(root).Split(new[] { "PREFABS" }, StringSplitOptions.RemoveEmptyEntries);
                    if (path.Length == 2)
                    {
                        ArrayUtils.Add(ref prefabList.Prefabs, new EntityPrefabsListAsset.EntityPrefabItem {
                            Path   = path[1].Trim('/').Replace(".prefab", ""),
                            Prefab = root
                        });
                    }
                }
            }

            EditorUtility.SetDirty(prefabList);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }