Beispiel #1
0
        internal void LoadBinaryAsset()
        {
            UseGUIDs.Clear();

            //var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
            //for (var i = 0;i < assets.Length; i++){
            //	Debug.Log(i + " : "+ assets[i].name + ":" + assets[i].GetType() + "\n" +
            //		//EditorUtility.GetAssetPath(assets[i]) + "\n"
            //		assets[i].GetHashCode()
            //	);
            //}

            var assetData = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));

            if (assetData is GameObject)
            {
                type = FR2_AssetType.MODEL;
                LoadGameObject(assetData as GameObject);
            }
            else if (assetData is TerrainData)
            {
                type = FR2_AssetType.TERRAIN;
                LoadTerrainData(assetData as TerrainData);
            }

            //Debug.Log("LoadBinaryAsset :: " + assetData + ":" + type);

            assetData = null;
            FR2_Unity.UnloadUnusedAssets();
        }
Beispiel #2
0
        // ----------------------------- REPLACE GUIDS ---------------------------------------

        internal bool ReplaceReference(string fromGUID, string toGUID)
        {
            if (IsMissing)
            {
                return(false);
            }

            if (IsReferencable)
            {
                var text = string.Empty;

                if (!File.Exists(assetPath))
                {
                    state = FR2_AssetState.MISSING;
                    return(false);
                }

                try
                {
                    text = File.ReadAllText(assetPath).Replace("\r", "\n");
                    File.WriteAllText(assetPath, text.Replace(fromGUID, toGUID));
                    return(true);
                }
                catch (Exception e)
                {
                    state = FR2_AssetState.MISSING;
//#if FR2_DEBUG
                    Debug.LogWarning("Replace Reference error :: " + e + "\n" + assetPath);
//#endif
                }

                return(false);
            }

            if (type == FR2_AssetType.TERRAIN)
            {
                var fromObj = FR2_Unity.LoadAssetWithGUID <Object>(fromGUID);
                var toObj   = FR2_Unity.LoadAssetWithGUID <Object>(toGUID);
                var found   = 0;
                var terrain = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object)) as TerrainData;

                if (fromObj is Texture2D)
                {
                    var arr = terrain.detailPrototypes;
                    for (var i = 0; i < arr.Length; i++)
                    {
                        if (arr[i].prototypeTexture == (Texture2D)fromObj)
                        {
                            found++;
                            arr[i].prototypeTexture = (Texture2D)toObj;
                        }
                    }

                    terrain.detailPrototypes = arr;

                    var arr3 = terrain.splatPrototypes;
                    for (var i = 0; i < arr3.Length; i++)
                    {
                        if (arr3[i].texture == (Texture2D)fromObj)
                        {
                            found++;
                            arr3[i].texture = (Texture2D)toObj;
                        }

                        if (arr3[i].normalMap == (Texture2D)fromObj)
                        {
                            found++;
                            arr3[i].normalMap = (Texture2D)toObj;
                        }
                    }

                    terrain.splatPrototypes = arr3;
                }

                if (fromObj is GameObject)
                {
                    var arr2 = terrain.treePrototypes;
                    for (var i = 0; i < arr2.Length; i++)
                    {
                        if (arr2[i].prefab == (GameObject)fromObj)
                        {
                            found++;
                            arr2[i].prefab = (GameObject)toObj;
                        }
                    }

                    terrain.treePrototypes = arr2;
                }

                EditorUtility.SetDirty(terrain);
                AssetDatabase.SaveAssets();

                fromObj = null;
                toObj   = null;
                terrain = null;
                FR2_Unity.UnloadUnusedAssets();

                return(found > 0);
            }

            Debug.LogWarning("Something wrong, should never be here - Ignored <" + assetPath +
                             "> : not a readable type, can not replace ! " + type);
            return(false);
        }
Beispiel #3
0
        private static void ApplicationUpdate()
        {
            bool notComplete = listReplace.Where(x => x.Value.processIndex >= 0).Count() > 0;

            if (lstThreads.Count <= 0 && notComplete)
            {
                foreach (KeyValuePair <string, ProcessReplaceData> item in listReplace)
                {
                    if (item.Value.processIndex >= 0)
                    {
                        ReplaceData a = item.Value.datas[item.Value.processIndex--];
                        a.isTerrian = a.asset.type == FR2_AssetType.TERRAIN;
                        if (a.isTerrian)
                        {
                            a.terrainData =
                                AssetDatabase.LoadAssetAtPath(a.asset.assetPath, typeof(Object)) as TerrainData;
                        }

                        var thread = new Thread(
                            delegate()
                        {
                            lock (a)
                            {
                                a.isSucess = a.asset.ReplaceReference(a.from, a.to, a.terrainData);
                                // Debug.Log(a.asset.assetPath+" "+ a.isSucess);
                            }
                        });
                        lstThreads.Add(thread);
                        thread.Start();
                    }
                }
            }

            for (int i = lstThreads.Count - 1; i >= 0; i--)
            {
                if (!lstThreads[i].IsAlive)
                {
                    lstThreads.RemoveAt(i);
                }
            }

            foreach (KeyValuePair <string, ProcessReplaceData> item in listReplace)
            {
                List <ReplaceData> lst = item.Value.datas;
                for (var i = 0; i < lst.Count; i++)
                {
                    ReplaceData data = lst[i];
                    if (!data.isUpdated && data.isSucess)
                    {
                        data.isUpdated = true;
                        if (data.isTerrian)
                        {
                            EditorUtility.SetDirty(data.terrainData);
                            AssetDatabase.SaveAssets();
                            data.terrainData = null;
                            FR2_Unity.UnloadUnusedAssets();
                        }
                        else
                        {
                            AssetDatabase.ImportAsset(data.asset.assetPath, ImportAssetOptions.Default);
                        }
                    }
                }
            }


            if (lstThreads.Count <= 0 && !notComplete)             //complete
            {
                var guidsRefreshed = new HashSet <string>();
                EditorApplication.update -= ApplicationUpdate;
                foreach (KeyValuePair <string, ProcessReplaceData> item in listReplace)
                {
                    List <ReplaceData> lst = item.Value.datas;
                    for (var i = 0; i < lst.Count; i++)
                    {
                        ReplaceData data = lst[i];
                        if (data.isSucess && !guidsRefreshed.Contains(data.asset.guid))
                        {
                            guidsRefreshed.Add(data.asset.guid);
                            FR2_Cache.Api.RefreshAsset(data.asset.guid, true);
                        }
                    }
                }

                lstThreads  = null;
                listReplace = null;
                FR2_Cache.Api.RefreshSelection();
                FR2_Cache.Api.Check4Usage();
                // EditorUtility.SetDirty(FR2_Cache.Api);
                AssetDatabase.Refresh();
                IsMergeProcessing = false;
            }
        }