123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- namespace TerrainComposer2
- {
- static public class TC
- {
- static private int refreshOutputReferences;
- static public bool refreshPreviewImages;
- static public bool repaintNodeWindow;
- static public List<MessageCommand> messages = new List<MessageCommand>();
- static public float autoGenerateCallTimeStart;
- public const int splatLimit = 16;
- public const int grassLimit = 16;
- public const int heightOutput = 0;
- public const int splatOutput = 1;
- public const int colorOutput = 2;
- public const int treeOutput = 3;
- public const int grassOutput = 4;
- public const int objectOutput = 5;
- public const int allOutput = 6;
- public const int nodeLabelLength = 19;
- static readonly public string[] outputNames = new string[6] { "Height", "Splat", "Color", "Tree", "Grass", "Object" };
- static readonly public string[] colChannelNames = new string[4] { "Red", "Green", "Blue", "Alpha" };
- static readonly public string[] colChannelNamesLowerCase = new string[4] { "red", "green", "blue", "alpha" };
- static readonly public Color[] colChannel = new Color[4] { new Color(1, 0.60f, 0.60f, 1), new Color(0.60f, 1, 0.60f, 1), new Color(0.60f, 0.60f, 1, 1), new Color(1, 1, 1, 1) };
- static public string installPath;
- static public string fullInstallPath;
- static public Type FindRTP()
- {
- Type t = Type.GetType("ReliefTerrain");
- TC_Settings.instance.isRTPDetected = t != null ? true : false;
- return t;
- }
- static public float GetVersionNumber()
- {
- return 2.6f;
- }
-
- static public int OutputNameToOutputID(string outputName)
- {
- for (int i = 0; i < outputNames.Length; i++) if (outputName == outputNames[i]) return i;
- return -1;
- }
- static public void AutoGenerate(bool waitForNextFrame = true)
- {
- AutoGenerate (new Rect (0,0,1,1), waitForNextFrame);
- }
- static public void AutoGenerate(Rect generateRect, bool waitForNextFrame = true)
- {
- // Debug.Log("Auto Generate");
- if (TC_Generate.instance != null)
- {
- TC_Generate.instance.cmdGenerate = true;
- TC_Generate.instance.autoGenerateRect = Mathw.ClampRect (generateRect, new Rect (0,0,1,1));
- }
- }
- static public void RefreshOutputReferences(int outputId)
- {
- // Debug.Log("Call refresh " + outputId);
- refreshOutputReferences = outputId;
- }
- static public int GetRefreshOutputReferences() { return refreshOutputReferences; }
- static public void RefreshOutputReferences(int outputId, bool autoGenerate)
- {
- // Debug.Log("Call refresh " + outputId);
- refreshOutputReferences = outputId;
-
- if (autoGenerate) AutoGenerate();
- }
- static public void Swap<T>(ref T source, ref T dest)
- {
- T temp = source;
- source = dest;
- dest = temp;
- }
- static public void Swap<T>(List<T> source, int indexS, List<T> dest, int indexD)
- {
- if (indexD < 0 || indexD >= dest.Count) return;
- T temp = source[indexS];
- source[indexS] = dest[indexD];
- dest[indexD] = temp;
- }
- static public void Swap<T>(ref T[] source, ref T[] dest)
- {
- for (int i = 0; i < source.Length; i++) Swap(ref source[i], ref dest[i]);
- }
- static public void InitArray<T>(ref T[] array, int resolution)
- {
- if (array == null) array = new T[resolution];
- else if (array.Length != resolution) array = new T[resolution];
- }
- static public void InitArray<T>(ref T[,] array, int resolutionX, int resolutionY)
- {
- if (array == null) array = new T[resolutionX, resolutionY];
- else if (array.Length != resolutionX * resolutionY) array = new T[resolutionX, resolutionY];
- }
- static public void DestroyChildrenTransform(Transform t)
- {
- int childCount = t.childCount;
- for (int i = 0; i < childCount; i++)
- {
- #if UNITY_EDITOR
- UnityEngine.GameObject.DestroyImmediate(t.GetChild(childCount - i - 1).gameObject);
- #else
- UnityEngine.GameObject.Destroy(t.GetChild(childCount - i - 1).gameObject);
- #endif
- }
- }
- static public void MoveToDustbinChildren(Transform t, int index)
- {
- int childCount = t.childCount;
- if (childCount >= index)
- {
- int length = childCount - index;
- for (int i = 0; i < length; i++) MoveToDustbin(t.GetChild(t.childCount - 1));
- }
- }
- static public void SetTextureReadWrite(Texture2D tex)
- {
- if (tex == null) return;
- #if UNITY_EDITOR
- string path = UnityEditor.AssetDatabase.GetAssetPath(tex);
- UnityEditor.TextureImporter textureImporter = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(path);
- if (textureImporter != null)
- {
- if (!textureImporter.isReadable)
- {
- textureImporter.isReadable = true;
-
- UnityEditor.AssetDatabase.ImportAsset(path, UnityEditor.ImportAssetOptions.ForceUpdate);
- }
- }
- #endif
- }
- static public string GetFileName(string path)
- {
- int index = path.LastIndexOf("/");
- if (index != -1)
- {
- string file = path.Substring(index + 1);
- index = file.LastIndexOf(".");
- if (index != -1) return file.Substring(0, index);
- return "";
- }
- return "";
- }
- static public string GetPath(string path)
- {
- int index = path.LastIndexOf("/");
- if (index != -1) return path.Substring(0, index);
- return "";
- }
- static public string GetAssetDatabasePath(string path)
- {
- return path.Replace(Application.dataPath, "Assets");
- }
- static public bool FileExists(string fullPath)
- {
- if (fullPath == null) { Debug.Log("Path doesn't exists."); return false; }
- System.IO.FileInfo file_info = new System.IO.FileInfo(fullPath);
- if (file_info.Exists) return true; else return false;
- }
- static public bool FileExistsPath(string path)
- {
- if (path == null) { Debug.Log("Path doesn't exists."); return false; }
- path = Application.dataPath.Replace("Assets", "") + path;
- // Debug.Log(path);
- System.IO.FileInfo file_info = new System.IO.FileInfo(path);
- if (file_info.Exists) return true; else return false;
- }
- static public long GetFileLength(string fullPath)
- {
- #if !UNITY_WEBPLAYER
- System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullPath);
- return fileInfo.Length;
- #else
- return 0;
- #endif
- }
- static public void GetInstallPath()
- {
- #if UNITY_EDITOR
- if (TC_Settings.instance != null)
- {
- installPath = UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.MonoScript.FromMonoBehaviour(TC_Settings.instance));
- installPath = installPath.Replace("/Scripts/Settings/TC_Settings.cs", "");
- }
- #endif
- }
- static public bool LoadGlobalSettings()
- {
- #if UNITY_EDITOR
-
- string file = Application.dataPath.Replace("Assets", "") + installPath + "/Defaults/";
- if (!FileExists(file + "TC_GlobalSettings.asset"))
- {
- UnityEditor.FileUtil.CopyFileOrDirectory(file + "TC_GlobalSettingsDefault.asset", file + "TC_GlobalSettings.asset");
- UnityEditor.AssetDatabase.Refresh();
- }
- GameObject go = GameObject.Find("TerrainComposer2");
- Transform settingsT = go.transform.Find("Settings");
- if (settingsT != null)
- {
- TC_Settings settings = settingsT.GetComponent<TC_Settings>();
- if (settings != null)
- {
- settings.global = UnityEditor.AssetDatabase.LoadAssetAtPath(installPath + "/Defaults/TC_GlobalSettings.asset", typeof(TC_GlobalSettings)) as TC_GlobalSettings;
- if (settings.global == null) return false;
- }
- else return false;
- }
- else return false;
- #endif
- return true;
- }
- static public void MoveToDustbin(Transform t)
- {
- TC_Settings settings = TC_Settings.instance;
- if (settings.dustbinT == null) settings.CreateDustbin();
- t.parent = settings.dustbinT;
- AddMessage(t.name + " is not compatible with the hierarchy of TerrainComposer\n It is moved to the 'Dustbin' GameObject.", 0);
- AddMessage("If you pressed the delete key you can undo it with control-z", 3);
- }
- static public void AddMessage(string message, float delay = 0, float duration = 2)
- {
- for (int i = 0; i < messages.Count; i++) if (messages[i].message.Contains(message)) return;
- messages.Add(new MessageCommand(message, delay, duration));
- }
- static public bool VerifyPortal(TC_ItemBehaviour item)
- {
- if (item == null) return false;
- if (item.outputId == heightOutput) return true;
- if (item is TC_Node || item is TC_NodeGroup) return true;
-
- AddMessage("This node cannot be used as a portal. All nodes in Height output can be used. And for the rest of the outputs only Nodes and NodeGroups can be used.");
- return false;
- }
- public class MessageCommand
- {
- public string message;
- public float delay;
- public float duration;
- public float startTime;
-
- public MessageCommand(string message, float delay, float duration)
- {
- this.message = message;
- this.delay = delay;
- this.duration = duration;
- startTime = Time.realtimeSinceStartup;
- }
- }
- }
- }
|