UnityEssentials
Small but useful tools and features for Unity
Utils.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using UnityEditor;
6 using System.IO;
7 
8 namespace UnityEngine
9 {
14  public static class Utils
15  {
16 
17  #region Types
18 
25  {
26  return GetTypeImplementations<T>().Where(impl=>!impl.IsSubclassOf(typeof(UnityEngine.Object))).ToArray();
27  }
28 
34  public static IEnumerable<Type> GetTypeImplementations<T>()
35  {
36  IEnumerable<Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes());
37 
38  Type interfaceType = typeof(T);
39  return types.Where(p => interfaceType.IsAssignableFrom(p) && !p.IsAbstract).ToArray();
40  }
41 
42  #endregion
43 
44  #region Editor
45  #if UNITY_EDITOR
46 
47  #region GetMainWindowCenteredPosition
48 
49 
55  public static Rect GetEditorWindowCenteredPosition(Vector2 windowSize)
56  {
57  Rect mainWindowRect = GetEditorMainWindowPos();
58  return GetCenteredWindowPosition(mainWindowRect, windowSize);
59  }
60 
61 
62  private static UnityEngine.Object sMainWindow = null;
63  private static Rect GetEditorMainWindowPos()
64  {
65  if (sMainWindow == null)
66  {
67  var containerWinType = AppDomain.CurrentDomain.GetAllDerivedTypes(typeof(ScriptableObject)).FirstOrDefault(t => t.Name == "ContainerWindow");
68  if (containerWinType == null)
69  throw new MissingMemberException("Can't find internal type ContainerWindow. Maybe something has changed inside Unity");
70  var showModeField = containerWinType.GetField("m_ShowMode", BindingFlags.NonPublic | BindingFlags.Instance);
71  if (showModeField == null)
72  throw new MissingFieldException("Can't find internal fields 'm_ShowMode'. Maybe something has changed inside Unity");
73  var windows = Resources.FindObjectsOfTypeAll(containerWinType);
74  foreach (var win in windows)
75  {
76  int showMode = (int)showModeField.GetValue(win);
77  if (showMode == 4) // main window
78  {
79  sMainWindow = win;
80  break;
81  }
82  }
83  }
84 
85  if (sMainWindow == null)
86  return new Rect(0, 0, 800, 600);
87 
88  var positionProperty = sMainWindow.GetType().GetProperty("position", BindingFlags.Public | BindingFlags.Instance);
89  if (positionProperty == null)
90  throw new MissingFieldException("Can't find internal fields 'position'. Maybe something has changed inside Unity.");
91  return (Rect)positionProperty.GetValue(sMainWindow, null);
92  }
93 
94  private static Type[] GetAllDerivedTypes(this AppDomain aAppDomain, Type aType)
95  {
96  return TypeCache.GetTypesDerivedFrom(aType).ToArray();
97  }
98 
99  private static Rect GetCenteredWindowPosition(Rect parentWindowPosition, Vector2 size)
100  {
101  var pos = new Rect
102  {
103  x = 0, y = 0,
104  width = Mathf.Min(size.x, parentWindowPosition.width * 0.90f),
105  height = Mathf.Min(size.y, parentWindowPosition.height * 0.90f)
106  };
107  float w = (parentWindowPosition.width - pos.width) * 0.5f;
108  float h = (parentWindowPosition.height - pos.height) * 0.5f;
109  pos.x = parentWindowPosition.x + w;
110  pos.y = parentWindowPosition.y + h;
111  return pos;
112  }
113 
114 
115  #endregion
116 
117 
118 
119  #endif
120  #endregion
121 
122 
123  #region Project
124 
129  public static string GetProjectName()
130  {
131  string[] s = Application.dataPath.Split('/');
132  string projectName = s[s.Length - 2];
133  return projectName;
134  }
135 
136  #endregion
137 
138  #region System
139 
140 
141 
142  #endregion
147  public static bool IsIOSupported()
148  {
149  return Application.platform != RuntimePlatform.WebGLPlayer &&
150  Application.platform != RuntimePlatform.WSAPlayerARM &&
151  Application.platform != RuntimePlatform.WSAPlayerX64 &&
152  Application.platform != RuntimePlatform.WSAPlayerX86 &&
153  Application.platform != RuntimePlatform.tvOS &&
154  Application.platform != RuntimePlatform.PS4;
155  }
156 
162  public static bool IsFilePath(string str)
163  {
164  bool result = false;
165  #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
166  if (Path.IsPathRooted(str))
167  {
168  try
169  {
170  string fullPath = Path.GetFullPath(str);
171  result = true;
172  }
173  catch (System.Exception)
174  {
175  result = false;
176  }
177  }
178  #endif
179  return result;
180  }
181 
182 
183 
184 
185  }
186 }
A collection of useful methods that did not fit into any of the other sections of the asset.
Definition: Utils.cs:15
static Type[] GetTypeImplementationsNotUnityObject< T >()
Find all implementations of the given parameter type except from those that are a subclass of 'UnityE...
Definition: Utils.cs:24
static bool IsFilePath(string str)
Determines if the string is file path.
Definition: Utils.cs:162
static IEnumerable< Type > GetTypeImplementations< T >()
Find all implementations of the given parameter type.
Definition: Utils.cs:34
static string GetProjectName()
Returns the name of the project
Definition: Utils.cs:129
static bool IsIOSupported()
Checks if the IO is supported on current platform or not.
Definition: Utils.cs:147