コード例 #1
0
 public static bool HasType(this Object unityObject, Type type)
 {
     if (type == typeof(GameObject))
     {
         return(unityObject.GetAsGameObject() != null);
     }
     else if (typeof(Component).IsAssignableFrom(type))
     {
         return(unityObject.GetAsGameObject()?.GetComponent(type) != null);
     }
     else
     {
         return(type.IsAssignableFrom(unityObject.GetType()));
     }
 }
コード例 #2
0
        public static string GetPath(Object obj)
        {
            var path       = string.Empty;
            var gameObject = obj.GetAsGameObject();

            if (gameObject)
            {
                while (gameObject.transform.parent)
                {
                    gameObject = gameObject.transform.parent.gameObject;
                    path      += gameObject.name + "/";
                }
            }

            return(path);
        }
コード例 #3
0
        public static Object GetAsObject(this Object unityObject, Type type)
        {
            if (type.IsAssignableFrom(unityObject.GetType()))
            {
                return(unityObject);
            }

            if (type == typeof(GameObject))
            {
                return(unityObject.GetAsGameObject());
            }

            if (typeof(Component).IsAssignableFrom(type))
            {
                return(unityObject.GetAsComponent(type));
            }

            return(null);
        }
コード例 #4
0
        public static T GetAsObject <T>(this Object unityObject) where T : Object
        {
            if (unityObject is T t)
            {
                return(t);
            }

            if (typeof(T) == typeof(GameObject))
            {
                return(unityObject.GetAsGameObject() as T);
            }

            if (typeof(Component).IsAssignableFrom(typeof(T)))
            {
                return(unityObject.GetAsComponent <T>());
            }

            return(null);
        }