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()));
     }
 }
Esempio n. 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);
        }
        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);
        }
        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);
        }