本文整理汇总了C#中UnityEngine.GameObject.GetComponents方法的典型用法代码示例。如果您正苦于以下问题:C# GameObject.GetComponents方法的具体用法?C# GameObject.GetComponents怎么用?C# GameObject.GetComponents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.GameObject
的用法示例。
在下文中一共展示了GameObject.GetComponents方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: enablePlayer
void enablePlayer(GameObject player)
{
foreach (MonoBehaviour obj in player.GetComponents<MonoBehaviour>())
obj.enabled = true;
foreach (CharacterController obj in player.GetComponents<CharacterController>())
obj.enabled = true;
}
示例2: CreateItemDataFromGameObject
private ItemData CreateItemDataFromGameObject(GameObject gameObject)
{
ValidateGameObject (gameObject);
ItemData itemData = new ItemData ();
itemData.transformData.position = gameObject.transform.position;
itemData.transformData.rotation = gameObject.transform.eulerAngles;
itemData.transformData.scale = gameObject.transform.localScale;
itemData.name = gameObject.name;
foreach (IPersistable persistable in gameObject.GetComponents<IPersistable>()) {
SerializableDictionary<string, object> componentConfiguration = new SerializableDictionary<string, object> ();
foreach (FieldInfo field in persistable.GetType().GetFields()) {
componentConfiguration.Add (field.Name, field.GetValue (persistable));
}
string componentName = persistable.GetType ().FullName;
itemData.componentData.configurations.Add (componentName, componentConfiguration);
}
foreach (Transform child in gameObject.transform) {
if (child.GetComponents<IPersistable> ().Length > 0) {
itemData.children.Add (CreateItemDataFromGameObject (child.gameObject));
}
}
return itemData;
}
示例3: AddGameObject
/// <summary>
/// This adds a new target game object.
/// </summary>
/// <param name="gameObject">
/// The game object to target.
/// </param>
public void AddGameObject(GameObject gameObject)
{
//make sure this game object is not null and is not already in the list
if (gameObject != null && targetGameObjects.Contains(gameObject) == false)
{
//warn if the game object being added is the parent of this component
if (gameObject == this)
{
Debug.LogWarning("You are adding a game object to a modifier that is the parent of this modifier. This may have unexpected results.");
}
//add the game object
targetGameObjects.Add(gameObject);
//get all MonoBehavior components on this target game object
MonoBehaviour[] behaviours = gameObject.GetComponents<MonoBehaviour>();
for (int j = 0; j < behaviours.Length; j++)
{
//make sure the found behavior is not this behavior, and it is a IVisModifierTarget
if (behaviours[j] != this && behaviours[j] is IVisModifierTarget)
{
//get the modifier target and add to the ValueUpdate event
IVisModifierTarget modifierTarget = behaviours[j] as IVisModifierTarget;
if (modifierTarget != null)
ValueUpdated += modifierTarget.OnValueUpdated;
}
}
}
}
示例4: SIHJR_PVFS_Grid
public SIHJR_PVFS_Grid(GameObject _boundary, float influenceWidth)
{
Collider2D collider = _boundary.GetComponents<Collider2D> () [0];
_bounds = collider.bounds;
_left = collider.bounds.min.x;
_bottom = collider.bounds.min.y;
_right = collider.bounds.max.x;
_top = collider.bounds.max.y;
_influenceWidth = influenceWidth;
//create new grid
_gridWidthCellCount = (int)Mathf.Ceil (collider.bounds.size.x / _influenceWidth);
_gridHeightCellCount = (int)Mathf.Ceil (collider.bounds.size.y / _influenceWidth);
//Debug.Log ("gridW: " + _gridWidthCellCount + " -- gridH: " + _gridHeightCellCount);
//Debug.Log ("grid L: " + _left + " -- grid R: " + _right);
//Debug.Log ("grid Cell Width: " + _influenceWidth);
_gridArray = new IList[_gridWidthCellCount + 1, _gridHeightCellCount + 1];
/*
_particles = new LinkedList<List<List<SIHJR_PVFS_Particle>>>(xCount);
for (int i = 0; i < xCount; i++) {
//create list
_particles[i] = new LinkedList<List<SIHJR_PVFS_Particle>>(yCount);
for (int j = 0; j < yCount; j++) {
_particles[j] = new LinkedList<SIHJR_PVFS_Particle>();
}
//create nested lists
}
*/
}
示例5: GetGameObjectBehaviors
public static string GetGameObjectBehaviors(GameObject gameObject)
{
if (gameObject == null)
{
return "GameObject is null";
}
var components = gameObject.GetComponents<Component>();
string list = "";
foreach (var component in components)
{
string behaviorString = "";
UnityEngine.Behaviour behavior = component as UnityEngine.Behaviour;
if (behavior != null)
{
behaviorString = " (Behavior)";
if (!behavior.enabled)
{
behaviorString += " (disabled)";
}
}
list += "Type: " + component.GetType() + behaviorString + "\n";
}
return list;
}
示例6: GetInjectableComponentsBottomUp
// NOTE: This method will not return components that are within a GameObjectContext
public static IEnumerable<Component> GetInjectableComponentsBottomUp(
GameObject gameObject, bool recursive)
{
var context = gameObject.GetComponent<GameObjectContext>();
if (context != null)
{
yield return context;
yield break;
}
if (recursive)
{
foreach (Transform child in gameObject.transform)
{
foreach (var component in GetInjectableComponentsBottomUp(child.gameObject, recursive))
{
yield return component;
}
}
}
foreach (var component in gameObject.GetComponents<Component>())
{
yield return component;
}
}
示例7: ApplyRecursive
private void ApplyRecursive(GameObject go)
{
foreach(Graphic graphic in go.GetComponents<Graphic>())
style.Apply(graphic);
foreach(Selectable selectable in go.GetComponents<Selectable>())
style.Apply(selectable);
foreach(Transform t in go.transform)
{
if(t.gameObject.GetComponent<pb_GUIStyleApplier>() != null)
continue;
ApplyRecursive(t.gameObject);
}
}
示例8: PlaySoundOn
//Beware: This assumes that it's safe to just play sounds using free audio sources.
// It's a replacement for NGUITools.PlaySound
public static AudioSource PlaySoundOn(AudioClip clip, GameObject go, float volume = 1.0f)
{
if (clip == null || go == null)
{
return null;
}
AudioSource[] sources = go.GetComponents<AudioSource>();
AudioSource source = null;
for (int i=0; i<sources.Length; ++i)
{
source = sources[i];
if (!source.isPlaying || source.clip == clip)
{
source.PlayClip(clip, volume);
return source;
}
}
source = go.AddComponent<AudioSource>();
source.playOnAwake = false;
source.PlayClip(clip, volume);
return source;
}
示例9: SetVisiible
void SetVisiible(GameObject obj, bool visible)
{
foreach (Renderer component in obj.GetComponents<Renderer>())
{
component.enabled = visible;
}
}
示例10: GameObjectToXmlElement
private static XmlElement GameObjectToXmlElement(GameObject gameObject, XmlDocument document)
{
XmlElement entityElement = document.CreateElement("entity");
entityElement.SetAttribute("name",gameObject.name);
Object[] outputComponents = gameObject.GetComponents(typeof(OutputComponent));
for (int j = 0; j < outputComponents.Length; j++)
{
OutputComponent oc = (OutputComponent)outputComponents[j];
XmlElement outputElement = oc.ToXmlElement(document);
entityElement.AppendChild(outputElement);
outputElement.SetAttribute("uid","" + oc.uid);
}
for (int i = 0; i < gameObject.transform.GetChildCount(); i++)
{
GameObject child = gameObject.transform.GetChild(i).gameObject;
int childOutputComponents = child.GetComponents(typeof(OutputComponent)).Length;
if (childOutputComponents > 0)
{
XmlElement childXmlElement = GameObjectToXmlElement(child, document);
entityElement.AppendChild(childXmlElement);
}
}
return entityElement;
}
示例11: InjectGameObject
public static void InjectGameObject(DiContainer container, GameObject gameObj)
{
foreach (var monoBehaviour in gameObj.GetComponents<MonoBehaviour>())
{
InjectMonoBehaviour(container, monoBehaviour);
}
}
示例12: FindInGO
private static void FindInGO(GameObject g)
{
go_count++;
Component[] components = g.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
components_count++;
if (components[i] == null)
{
missing_count++;
string s = g.name;
Transform t = g.transform;
while (t.parent != null)
{
s = t.parent.name +"/"+s;
t = t.parent;
}
Debug.Log (s + " has an empty script attached in position: " + i, g);
}
}
// Now recurse through each child GO (if there are any):
foreach (Transform childT in g.transform)
{
//Debug.Log("Searching " + childT.name + " " );
FindInGO(childT.gameObject);
}
}
示例13: Start
// Use this for initialization
void Start () {
ConsoleLog.SLog ("PlayerGameManager Start()");
anim = GetComponent<Animator> ();
cardboardCamera = GameObject.FindGameObjectWithTag("PlayerHead");
cardboardHead = cardboardCamera.GetComponent<CardboardHead> ();
headPos = GameObject.FindGameObjectWithTag ("CameraPos").transform;
gun = GameObject.FindGameObjectWithTag ("MyGun");
gunProperties = gun.GetComponent<GunProperties> ();
gunAudio = gun.GetComponents<AudioSource> ();
gunLight = GameObject.FindGameObjectWithTag ("GunLight");
gunFlashEmitter = GameObject.FindGameObjectWithTag ("GunFlash").GetComponent<EllipsoidParticleEmitter>();
gunFlashEmitter.emit = false;
footstepsAudio = GetComponent<AudioSource> ();
bulletHoleArray = new ArrayList (bulletHoleMaxAmount);
//HUD
HUD = GameObject.FindGameObjectWithTag("HUD");
healthBar = HUD.transform.GetChild (0) as Transform;
bulletText = HUD.transform.GetChild (1).GetComponent<TextMesh>();
reloadText = HUD.transform.GetChild (2).GetComponent<TextMesh>();
grenadeText = HUD.transform.GetChild (3).GetComponent<TextMesh>();
HUDCanvas = HUD.transform.GetChild (4).gameObject;
deadText = HUDCanvas.transform.GetChild (0).gameObject;
endRoundText = HUDCanvas.transform.GetChild (1).gameObject;
endGameText = HUDCanvas.transform.GetChild (2).gameObject;
bulletText.text = gunProperties.bulletLoadCurrent + "/" + gunProperties.bulletStoreCurrent;
grenadeText.text = grenadeStore + "";
}
示例14: Setup
public void Setup(GameObject targetType, int inputNumber)
{
if (playerShip != null)
{
Destroy(playerShip);
playerShip = null;
}
playerShip = (GameObject)Instantiate(targetType, Vector3.zero, Quaternion.identity);
var scripts = playerShip.GetComponents<MonoBehaviour>();
for (int i = 0; i < scripts.Length; i++)
{
MonoBehaviour data = scripts[i];
Controller controller = data as Controller;
if (controller != null)
{
controller.horizontalAxis = "Horizontal"+inputNumber;
controller.verticalAxis = "Vertical"+inputNumber;
controller.accelerate = "Accelerate" + inputNumber;
controller.otherAxis = "Other"+inputNumber;
controller.AssignCamera(GetComponent<Camera>());
}
}
}
示例15: CacheMethodsForGameObject
private void CacheMethodsForGameObject(GameObject go, System.Type parameterType) {
List<string> cachedMethods = new List<string>();
cache[go].Add( parameterType, cachedMethods );
List<System.Type> addedTypes = new List<System.Type>();
MonoBehaviour[] behaviours = go.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour beh in behaviours) {
System.Type type = beh.GetType();
if (addedTypes.IndexOf(type) == -1) {
System.Reflection.MethodInfo[] methods = type.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
foreach (System.Reflection.MethodInfo method in methods) {
// Only add variables added by user, i.e. we don't want functions from the base UnityEngine baseclasses or lower
string moduleName = method.DeclaringType.Assembly.ManifestModule.Name;
if (!moduleName.Contains("UnityEngine") && !moduleName.Contains("mscorlib") &&
!method.ContainsGenericParameters &&
System.Array.IndexOf(ignoredMethodNames, method.Name) == -1) {
System.Reflection.ParameterInfo[] paramInfo = method.GetParameters();
if (paramInfo.Length == 0) {
cachedMethods.Add(method.Name);
}
else if (paramInfo.Length == 1 && paramInfo[0].ParameterType == parameterType) {
cachedMethods.Add(method.Name);
}
}
}
}
}
}