Skip to content

Instantly share code, notes, and snippets.

@drawcode
Created December 10, 2012 22:01
Show Gist options
  • Save drawcode/4253758 to your computer and use it in GitHub Desktop.
Save drawcode/4253758 to your computer and use it in GitHub Desktop.
Unity Extensions C#
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using Engine.Utility;
public static class GameObjectExtensions {
public static void SetLayerRecursively(this GameObject inst, int layer) {
inst.layer = layer;
foreach(Transform child in inst.transform)
child.gameObject.SetLayerRecursively(layer);
}
/// <summary>
/// Adds all the components found on a resource prefab.
/// </summary>
/// <param name='inst'>
/// Instance of game object to add the components to
/// </param>
/// <param name='path'>
/// Path of prefab relative to ANY resource folder in the assets directory
/// </param>
///
public static void AddComponentsFromResource(this GameObject inst, string path) {
var go = Resources.Load(path) as GameObject;
foreach(var src in go.GetComponents<Component>()) {
var dst = inst.AddComponent(src.GetType()) as Behaviour;
dst.enabled = false;
ComponentUtil.Copy(dst, src);
dst.enabled = true;
}
}
/// <summary>
/// Adds a component of the specific type found on a resource prefab.
/// </summary>
/// <returns>
/// The newly added component.
/// </returns>
/// <param name='inst'>
/// Instance of game object to add the component to
/// </param>
/// <param name='path'>
/// Path of prefab relative to ANY resource folder in the assets directory
/// </param>
/// <typeparam name='T'>
/// The type of component to find on the prefab and add.
/// </typeparam>
/// <exception cref='ArgumentException'>
/// Is thrown when the path is invalid.
/// </exception>
///
public static T AddComponentFromResource<T>(this GameObject inst, string path)
where T : Component {
var go = Resources.Load(path) as GameObject;
if(go == null)
throw new ArgumentException("Invalid component path", "path");
var src = go.GetComponent<T>();
var dst = inst.AddComponent<T>();
ComponentUtil.Copy(dst, src);
return dst;
}
/// <summary>
/// Gets a component from a game object (supports interfaces)
/// </summary>
/// <returns>
/// The component found in the game object
/// </returns>
/// <param name='inst'>
/// Instance of game object to add the component to
/// </param>
/// <typeparam name='T'>
/// The type of component, or interface, to find
/// </typeparam>
///
public static T GetComponent<T>(this GameObject inst)
where T : class {
return inst.GetComponent(typeof(T)) as T;
}
public static GameObject FindTypeAboveObject<T>(this GameObject inst)
where T : class {
if(inst == null) {
return null;
}
return FindTypeAboveObjectRecursive<T>(inst);
}
public static GameObject FindTypeAboveObjectRecursive<T>(this GameObject inst)
where T : class {
if(inst == null) {
return null;
}
if(inst != null) {
if(inst.GetComponent<T>() != null) {
return inst;
}
if(inst.transform.parent != null) {
return FindTypeAboveObjectRecursive<T>(inst.transform.parent.gameObject);
}
}
return null;
}
public static Transform FindBelow(this GameObject inst, string name) {
if(inst == null) {
return null;
}
if (inst.transform.childCount == 0) {
return null;
}
var child = inst.transform.Find(name);
if (child != null) {
return child;
}
foreach (GameObject t in inst.transform) {
child = FindBelow(t, name);
if (child != null) {
return child;
}
}
return null;
}
public static void Show(this GameObject inst) {
//LogUtil.Log("Show:" + inst.name);
if(inst != null) {
if(!inst.active) {
inst.SetActiveRecursively(true);
}
if(inst.renderer != null) {
if(!inst.renderer.enabled) {
inst.renderer.enabled = true;
}
}
}
}
public static void Hide(this GameObject inst) {
//LogUtil.Log("Hide:" + inst.name);
if(inst != null) {
if(inst.renderer != null) {
inst.renderer.enabled = false;
}
inst.SetActiveRecursively(false);
}
}
public static bool IsReady(this UnityEngine.Object inst) {
return inst != null ? true : false;
}
public static void StopSounds(this GameObject inst) {
if(inst == null)
return;
GameObjectHelper.StopSounds(inst);
}
public static void PauseSounds(this GameObject inst) {
if(inst == null)
return;
GameObjectHelper.PauseSounds(inst);
}
public static void PlaySounds(this GameObject inst) {
if(inst == null)
return;
GameObjectHelper.PlaySounds(inst);
}
public static bool IsRenderersVisible(this GameObject inst) {
if(inst == null)
return false;
return GameObjectHelper.IsRenderersVisible(inst);
}
public static bool IsAudioSourcePlaying(this GameObject inst) {
if(inst == null)
return false;
return GameObjectHelper.IsAudioSourcePlaying(inst);
}
public static void ShowRenderers(this GameObject inst) {
if(inst == null)
return;
GameObjectHelper.ShowRenderers(inst);
}
public static void HideRenderers(this GameObject inst) {
if(inst == null)
return;
GameObjectHelper.HideRenderers(inst);
}
public static void DestroyChildren(this GameObject inst) {
if(inst == null)
return;
List<Transform> transforms = new List<Transform>();// inst.transform.childCount;
int b = 0;
foreach(Transform t in inst.transform) {
transforms.Add(t);// = t;
b++;
}
foreach(Transform t in transforms) {
t.parent = null;
UnityEngine.Object.Destroy(t.gameObject);
}
transforms.Clear();
transforms = null;
}
public static void ChangeLayersRecursively(this GameObject inst, string name) {
if(inst == null)
return;
foreach (Transform child in inst.transform) {
child.gameObject.layer = LayerMask.NameToLayer(name);
ChangeLayersRecursively(child.gameObject, name);
}
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public static class GameObjectHelper
{
public static void StopSounds(GameObject inst) {
if(inst == null)
return;
if(inst.audio != null) {
if(inst.audio.isPlaying) {
inst.audio.Stop();
}
}
foreach(AudioSource source in inst.GetComponentsInChildren<AudioSource>()) {
source.Stop();
}
}
public static void PauseSounds(GameObject inst) {
if(inst == null)
return;
if(inst.audio != null) {
if(inst.audio.isPlaying) {
inst.audio.Pause();
}
}
foreach(AudioSource source in inst.GetComponentsInChildren<AudioSource>()) {
source.Pause();
}
}
public static void PlaySounds(GameObject inst) {
if(inst == null)
return;
if(inst.audio != null) {
if(!inst.audio.isPlaying) {
inst.audio.Play();
}
}
foreach(AudioSource source in inst.GetComponentsInChildren<AudioSource>()) {
source.Play();
}
}
public static bool IsAudioSourcePlaying(GameObject inst) {
if(inst == null)
return false;
if(inst.audio != null) {
if(inst.audio.isPlaying) {
return true;
}
}
foreach(AudioSource source in inst.GetComponentsInChildren<AudioSource>()) {
if(source.isPlaying) {
return true;
}
}
return false;
}
public static bool IsRenderersVisible(GameObject inst) {
if(inst == null)
return false;
if(inst.renderer != null) {
if(inst.renderer.enabled) {
return true;
}
}
Renderer[] rendererComponents = inst.GetComponentsInChildren<Renderer>();
// Enable rendering:
foreach (Renderer component in rendererComponents) {
if(component.enabled) {
return true;
}
}
return false;
}
public static void ShowRenderers(GameObject inst) {
if(inst == null)
return;
if(inst.renderer != null) {
inst.renderer.enabled = true;
}
Renderer[] rendererComponents = inst.GetComponentsInChildren<Renderer>();
// Enable rendering:
foreach (Renderer component in rendererComponents) {
component.enabled = true;
}
}
public static void HideRenderers(GameObject inst) {
if(inst == null)
return;
if(inst.renderer != null) {
inst.renderer.enabled = false;
}
Renderer[] rendererComponents = inst.GetComponentsInChildren<Renderer>();
// Enable rendering:
foreach (Renderer component in rendererComponents) {
component.enabled = false;
}
}
public static void DumpRootTransforms() {
UnityEngine.Object[] objs = UnityEngine.GameObject.FindSceneObjectsOfType(typeof(GameObject));
foreach(UnityEngine.Object obj in objs) {
GameObject go = obj as GameObject;
if(go.transform.parent == null) {
DumpGoToLog(go);
}
}
}
public static void DumpGoToLog(GameObject go) {
Debug.Log("DUMP: go:" + go.name + "::::" + GameObjectHelper.DumpGo(go));
}
public static string DumpGo(GameObject go) {
StringBuilder sb = new StringBuilder();
sb.Append(go.name);
DumpGameObject(go, sb, "", false);
return sb.ToString();
}
private static void DumpGameObject(GameObject gameObject, StringBuilder sb, string indent, bool includeAllComponents) {
bool rendererEnabled = false;
if(gameObject.renderer != null) {
rendererEnabled = gameObject.renderer.enabled;
}
int markerId = -1;
/*
if(gameObject.GetComponent<MarkerBehaviour>() != null) {
markerId = gameObject.GetComponent<MarkerBehaviour>().MarkerID;
}
*/
bool hasLoadedObj = false;
/*
if(gameObject.GetComponent<ARLoadedActionObject>() != null) {
hasLoadedObj = true;
}
*/
sb.Append(string.Format("\r\n{0}+{1} - a:{2} - r:{3} - mid:{4} - loadedObj: {5} - scale: x:{6} y:{7} z:{8} - pos: x:{9} y:{10} z:{11}",
indent, gameObject.name,
gameObject.active, rendererEnabled,
markerId, hasLoadedObj,
gameObject.transform.localScale.x,
gameObject.transform.localScale.y,
gameObject.transform.localScale.z,
gameObject.transform.position.x,
gameObject.transform.position.y,
gameObject.transform.position.z));
if(includeAllComponents) {
foreach (Component component in gameObject.GetComponents<Component>()) {
DumpComponent(component, sb, indent + " ");
}
}
foreach (Transform child in gameObject.transform) {
DumpGameObject(child.gameObject, sb, indent + " ", includeAllComponents);
}
}
private static void DumpComponent(Component component, StringBuilder sb, string indent) {
sb.Append(string.Format("{0}{1}", indent, (component == null ? "(null)" : component.GetType().Name)));
}
}
using System;
using UnityEngine;
public static class Vector3Extensions {
public static Vector3 WithX(this Vector3 inst, float x) {
inst.x = x;
return inst;
}
public static Vector3 WithY(this Vector3 inst, float y) {
inst.y = y;
return inst;
}
public static Vector3 WithZ(this Vector3 inst, float z) {
inst.z = z;
return inst;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment