SGResources
T SGResources.Load<T>(string path)
Parameters
path | Path to the target resource to load. |
Returns
T The requested asset's Type.
Description
Loads an asset stored at path
in a folder called SGResources.
Returns the asset at path
if it can be found otherwise returns null. Only an object of type T
will be returned. The path
is relative to any folder named SGResources
inside the Assets folder of your project. More than one SGResources folder can be used. For example, a project may have SGResources folders called Assets/SGResources
and Assets/Guns/SGResources
. String names that include Assets
and SGResources
are not needed. For example the loading of a GameObject at Assets/Guns/SGResources/Shotgun.prefab
does not use the folder names. Also, if Assets/SGResources/Guns/Missiles/PlasmaGun.prefab
exists it will be loaded using Prefabs/Missiles/PlasmaGun
. If you have multiple SGResources folders you cannot duplicate use of an asset name.
Another example of the SGResources
folder. In SGResources
there are two files, fancyA
and fancyB
. SGResources
also has Resources2
folder. This folder contains two files, fancyA2
and fancyB2
. Finally, Resources2
has a folder called Resources3
with a single file also called fancyB
. (This means the file in Resources3
has the same name as in SGResources
.) The files in SGResources
can be loaded as fancyA
and fancyB
with no need for the folder hierarchy Assets/SGResources
. Also, the files in Resources2
can be loaded. Loading these require the folder directory so an example load will be Resources2/fancyB2
. Finally, loading from Resources3
will be Resources2/Resources3/fancyB
.
Extensions must be omitted.
All asset names and paths in Unity use forward slashes, paths using backslashes will not work.
Object SGResources.Load(string path)
Object SGResources.Load(string path, System.Type type)
Parameters
path | Path to the target resource to load. When using an empty string (i.e., ""), the function loads the entire contents of the SGResources folder. |
systemTypeInstance | Type filter for objects returned. |
Returns
Object The requested asset returned as an Object.
Description
Loads an asset stored at path
in a SGResources folder.
Returns the asset at path
if it can be found, otherwise returns null. If the type
parameter is specified, only objects of this type are returned. The path
is relative to any SGResources folder inside the Assets folder of your project, extensions must be omitted.
Note: All asset names and paths in Unity use forward slashes, paths using backslashes will not work.
T[] SGResources.LoadAll(string path)
Parameters
path | Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the SGResources folder. |
Returns
List of all objects of Type T
.
Description
Loads all assets in a folder or file at path
in a SGResources folder.
If path
refers to a folder, all assets in the folder will be returned. If path
refers to a file, only that asset will be returned. Only objects of type T
will be returned. The path
is relative to any SGResources folder inside the Assets folder of your project.
Object[] SGResources.LoadAll(string path)
Object[] SGResources.LoadAll(string path, System.Type type)
Parameters
path | Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the SGResources folder. |
Returns
List of all objects of Type Object
.
Description
Loads all assets in a folder or file at path
in a SGResources folder.
If path
refers to a folder, all assets in the folder will be returned. If path
refers to a file, only that asset will be returned. The path
is relative to any SGResources folder inside the Assets folder of your project.
All asset names and paths in Unity use forward slashes. Paths using backslashes will not work.
T[] FindObjectsOfTypeAll<T>()
Returns
List of all objects of Type T
.
Description
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal objects, therefore be careful with the way you handle the returned objects.
Contrary to Object.FindObjectsOfType this function will also list disabled objects.
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ExampleScript : MonoBehaviour
{
List<GameObject> GetAllObjectsOnlyInScene()
{
List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
if (!EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
objectsInScene.Add(go);
}
return objectsInScene;
}
}
Object[] SGResources.FindObjectsOfTypeAll(System.Type type)
Returns
List of all objects of Type T
.
Description
This function using non-generic types can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore be careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.
That this function is very slow and is not recommended to be used every frame.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class ExampleScript : MonoBehaviour
{
List<UnityEngine.Object> GetSceneObjectsNonGeneric()
{
List<UnityEngine.Object> objectsInScene = new List<UnityEngine.Object>();
foreach (UnityEngine.Object go in Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)) as UnityEngine.Object[])
{
GameObject cGO = go as GameObject;
if (cGO != null && !EditorUtility.IsPersistent(cGO.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
objectsInScene.Add(go);
}
return objectsInScene;
}
}
No Comments