本文整理汇总了C#中UnityEngine.GameObject.AddComponent方法的典型用法代码示例。如果您正苦于以下问题:C# GameObject.AddComponent方法的具体用法?C# GameObject.AddComponent怎么用?C# GameObject.AddComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.GameObject
的用法示例。
在下文中一共展示了GameObject.AddComponent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Awake
private TriggerParent triggerParent; //this is a utility class, that lets us check if the player is close to the coins "bounds sphere trigger"
#endregion Fields
#region Methods
//private GUIManager gui;
//setup
void Awake()
{
//gui = FindObjectOfType(typeof(GUIManager)) as GUIManager ;
if(tag != "Pickup")
{
tag = "Pickup";
}
bubbleBlowing = GameObject.FindGameObjectWithTag("Player").GetComponent<BubbleBlowing>();
GetComponent<Collider>().isTrigger = true;
triggerParent = GetComponentInChildren<TriggerParent>();
//if no trigger bounds are attached to coin, set them up
if(!triggerParent)
{
GameObject bounds = new GameObject();
bounds.name = "Bounds";
bounds.AddComponent<SphereCollider>();
bounds.GetComponent<SphereCollider>().radius = 3f;
bounds.GetComponent<SphereCollider>().isTrigger = true;
bounds.transform.parent = transform;
bounds.transform.position = transform.position;
bounds.AddComponent<TriggerParent>();
triggerParent = GetComponentInChildren<TriggerParent>();
triggerParent.tagsToCheck = new string[1];
triggerParent.tagsToCheck[0] = "Player";
}
for(int i = 0; i < transform.childCount; i++)
{
this.transform.GetChild(i).gameObject.layer = LayerMask.NameToLayer("Items");
}
}
示例2: CreateDecal
public static GameObject CreateDecal(Material mat, Rect uvCoords, float scale)
{
GameObject decal = new GameObject();
decal.name = "Decal" + decal.GetInstanceID();
decal.AddComponent<MeshFilter>().sharedMesh = DecalMesh("DecalMesh" + decal.GetInstanceID(), mat, uvCoords, scale);
decal.AddComponent<MeshRenderer>().sharedMaterial = mat;
#if UNITY_5
decal.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
#else
decal.GetComponent<MeshRenderer>().castShadows = false;
#endif
qd_Decal decalComponent = decal.AddComponent<qd_Decal>();
decalComponent.SetScale(scale);
decalComponent.SetTexture( (Texture2D)mat.mainTexture );
decalComponent.SetUVRect(uvCoords);
#if DEBUG
decal.AddComponent<qd_DecalDebug>();
#endif
return decal;
}
示例3: DrawBarrier
void DrawBarrier()
{
for (int i = 0; i < points.Count; i++)
{
Vector3 p = points[i];
p -= new Vector3(0.5f, 0.5f, 0);
p.x *= (float) Screen.width / Screen.height;
p *= 15;
points[i] = p;
}
GameObject container = new GameObject();
container.transform.parent = topSideTran;
container.AddComponent<Mover>();
DrawDestruct dd = container.AddComponent<DrawDestruct>();
dd.cost = points.Count * energyCost;
dd.drawClass = this;
dd.bonusScore = 5;
Destroy(container, selfDestructTime);
for (int i = 0; i < points.Count - 1; i++)
{
GameObject obj = (GameObject) GameObject.Instantiate(linePrefab, points[i], Quaternion.identity);
obj.transform.position = Vector3.Lerp(points[i], points[i+1], 0.5f);
obj.tag = "Barrier";
obj.transform.LookAt(points[i+1]);
Vector3 s = obj.transform.localScale;
obj.transform.localScale = new Vector3(s.x, s.y, Vector3.Distance(points[i], points[i+1]));
obj.transform.parent = container.transform;
}
}
示例4: UseExistingOrCreateNewMainCamera
public static void UseExistingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if (Camera.mainCamera != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent("Camera");
tempCamera.tag = "MainCamera";
tempCamera.AddComponent("TP_Camera");
}
myCamera = tempCamera.GetComponent("TP_Camera") as TP_Camera;
targetLookAt = GameObject.Find("targetLookAt") as GameObject;
if (targetLookAt == null)
{
targetLookAt = new GameObject("targetLookAt");
targetLookAt.transform.position = Vector3.zero;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
示例5: AddHipCollider
private CapsuleCollider AddHipCollider(GameObject hipObject) {
Rigidbody rb = hipObject.AddComponent<Rigidbody>();
if (rb != null) {
rb.mass = 1;
rb.useGravity = false;
rb.isKinematic = true;
//rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
CapsuleCollider collider = hipObject.AddComponent<CapsuleCollider>();
if (collider != null) {
collider.isTrigger = true;
if (proximitySpeed) {
collider.height = 0.80f;
collider.radius = 1f;
} else {
collider.height = 1.60f;
collider.radius = 0.20f;
}
collider.center = new Vector3(-hipObject.transform.localPosition.x, 0.2f, -hipObject.transform.localPosition.z);
}
return collider;
}
示例6: BuildingSphereField
void BuildingSphereField()
{
for(int y=0; y<fieldHeight; y++)
{
for(int z=0; z<fieldDepth; z++)
{
shiftX = 0.0f;
for(int x=0; x<fieldWidth; x++){
int randomNumber = Random.Range(1,3);
float randomSize = Mathf.Round(Random.Range(0.3f,0.6f) * 100.0f) / 100.0f;
shiftX += (randomSize + 0.2f);
switch(randomNumber)
{
case 1: fieldPart = GameObject.CreatePrimitive (PrimitiveType.Sphere);
fieldPart.GetComponent<SphereCollider>().material = Resources.Load("darkroom/BouncingBob") as PhysicMaterial;
break;
case 2: fieldPart = GameObject.CreatePrimitive (PrimitiveType.Cube);
fieldPart.GetComponent<BoxCollider>().material = Resources.Load("darkroom/BouncingBob") as PhysicMaterial;
break;
}
fieldPart.transform.localScale = new Vector3 (randomSize, randomSize, randomSize);
fieldPart.GetComponent<MeshRenderer>().material = Resources.Load("darkroom/darkroom") as Material;
fieldPart.AddComponent<BallBehavior>();
Rigidbody rb = fieldPart.AddComponent<Rigidbody> ();
rb.useGravity = false;
fieldPart.transform.parent = transform;
fieldPart.transform.localPosition = new Vector3 (-shiftX, y*-0.7f, z*-0.7f);
fieldPart.name = "lightBall";
fieldPart.layer = LayerMask.NameToLayer("Ignore Raycast");
}
}
}
}
示例7: Substract
public void Substract()
{
var stopWatch = new Stopwatch();
stopWatch.Start();
var csgA = new CSG.CSG();
csgA.Construct(A.GetComponent<MeshFilter>().sharedMesh, A.transform, 0);
var csgB = new CSG.CSG();
csgB.Construct(B.GetComponent<MeshFilter>().sharedMesh, B.transform, 1);
var substract = csgA.Substract(csgB);
var newMesh = substract.ToMesh();
Result = new GameObject("Substract");
var defObj = Result.AddComponent<DefaultObject>();
var meshFilter = Result.AddComponent<MeshFilter>();
meshFilter.sharedMesh = newMesh;
var renderer = Result.AddComponent<MeshRenderer>();
renderer.sharedMaterials = new Material[] { A.GetComponent<MeshRenderer>().sharedMaterial, B.GetComponent<MeshRenderer>().sharedMaterial };
if (DeleteOriginal)
{
Object.DestroyImmediate(A);
Object.DestroyImmediate(B);
}
stopWatch.Stop();
defObj.GenerationTimeMS = stopWatch.ElapsedMilliseconds;
}
示例8: Awake
void Awake()
{
// create empty(transform only) game object in scene with default position (0,0,0)
GameObject myObj = new GameObject();
myObj.transform.position = new Vector3(0, 3, 0);
// add sprite renderer component
myObj.AddComponent<SpriteRenderer>();
// create texture from resource file
myTexture = Resources.Load<Texture2D>("ProceduralSprite/100_px") as Texture2D;
// A) store sprite > store component > assign spite to component property
mySprite = Sprite.Create(myTexture, new Rect(25,25,75,75), new Vector2(0f, 0f));
SpriteRenderer myObjComponent = myObj.GetComponent<SpriteRenderer>();
myObjComponent.sprite = mySprite;
// B) direct assign
// myObj.GetComponent<SpriteRenderer>().sprite = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0.5f, 0f));
// 1)----
// load previously created animator controller as runtimeAnimationConroller
myRTimeAnimator = Resources.Load<RuntimeAnimatorController>("ProceduralSprite/Cube") as RuntimeAnimatorController;
// add animator component to GO and assign animator controller
myObj.AddComponent<Animator>();
myObj.GetComponent<Animator>().runtimeAnimatorController = myRTimeAnimator;
// 2)----
// alternative way (direct assing)
// myObj.AddComponent<Animator>();
// myObj.GetComponent<Animator>().runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("ProceduralSprite/Cube") as RuntimeAnimatorController;
}
示例9: CreatePrefab
public void CreatePrefab()
{
if (prefabAtmosphere == null)
{
prefabAtmosphere = new GameObject ("RefractiveAtmosphere");
prefabAtmosphere.SetActive (false);
var mf = prefabAtmosphere.AddComponent<MeshFilter> ();
var mr = prefabAtmosphere.AddComponent<MeshRenderer> ();
mr.castShadows = false;
mr.receiveShadows = false;
var material = new Material (Shaders.RefractiveAtmosphere);
var _normals = new Texture2D (4, 4, TextureFormat.ARGB32, false);
_normals.LoadImage (Textures.RefractiveAtmosphereNormals);
material.SetTexture ("_BumpMap", _normals);
material.SetTextureScale ("_BumpMap", new Vector2 (4f, 4f));
material.SetVector ("_BumpMapOffset", new Vector4 (0, 0, 1, 0));
mr.sharedMaterial = material;
var sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
var sphereMesh = sphere.GetComponent<MeshFilter> ().mesh;
DestroyImmediate (sphere);
mf.sharedMesh = sphereMesh;
var behaviour = prefabAtmosphere.AddComponent<RefractiveAtmosphere> ();
prefabAtmosphere.transform.localScale = Vector3.one * 1250f;
DontDestroyOnLoad (prefabAtmosphere);
}
}
示例10: GetGUIText
public GUITextObj GetGUIText(int LayerNum)
{
int Count = 0;
GUITextObj TmpTextObj;
GameObject tmpGameObj;
//Search guitext buffer for available object
for (Count=0;Count<pTextBuffer.Count;Count++)
{
TmpTextObj = (GUITextObj) pTextBuffer[Count];
if (TmpTextObj.GUIActive == false)
{
//Flag the object as used
TmpTextObj.GUIActive = true;
//Set the objects layer
TmpTextObj.GameObj.layer = LayerNum;
//Return object
return TmpTextObj;
}
}
//No available object, so allocate more
int BuffIdx = 0;
int BuffSize = pTextBuffer.Count;
for (Count=0;Count<AllocBlockSize;Count++)
{
//Create new guitext object
tmpGameObj = new GameObject();
BuffIdx = BuffSize + Count;
tmpGameObj.name = "GUIText" + BuffIdx.ToString();
tmpGameObj.AddComponent("GUIText");
tmpGameObj.AddComponent("Animator");
tmpGameObj.AddComponent("GUITextObj");
//Set a child of guitextmgr
tmpGameObj.transform.parent = gameObject.transform;
//Add to buffer
TmpTextObj = (GUITextObj) tmpGameObj.GetComponent(typeof(GUITextObj));
TmpTextObj.GUIActive = false;
pTextBuffer.Add(TmpTextObj);
}
//Search guitext buffer for available object
for (Count=0;Count<pTextBuffer.Count;Count++)
{
TmpTextObj = (GUITextObj) pTextBuffer[Count];
if (TmpTextObj.GUIActive == false)
{
//Flag the object as used
TmpTextObj.GUIActive = true;
//Set the objects layer
TmpTextObj.GameObj.layer = LayerNum;
//Return object
return TmpTextObj;
}
}
return null;
}
示例11: initialise
public IStack initialise(Vector3 centroid, Vector3 stackTwist, float scale, Color stackColour)
{
stack = new GameObject("Torus");
torus = stack.AddComponent<TorusPrimitive>();
torus.OuterRadius = scale;
torus.InnerRadius = 0.2f * scale;
//torus.Sides = 32;
//torus.Segments = 32;
torus.UpdatePrimitive ();
stackRigidBody = stack.AddComponent<Rigidbody>();
stackRigidBody.useGravity = false;
stackRigidBody.detectCollisions = false;
stack.transform.position = centroid;
currentRotationX = stackTwist.x;
currentRotationY = stackTwist.y;
currentRotationZ = stackTwist.z;
stack.transform.Rotate (stackTwist);
stack.transform.localScale = new Vector3(scale, scale, scale);
stack.GetComponent<Renderer>().material.color = stackColour;
return this;
}
示例12: AddNewMoving
void AddNewMoving(GameObject obj)
{
Destroy (obj.GetComponent<Moving> ());
obj.AddComponent<RotateObject> ();
obj.AddComponent<JumpSlerpObject> ();
}
示例13: CreateCanvas
void CreateCanvas()
{
if( !_didCreateUiStuff )
{
_didCreateUiStuff = true;
//Create the UI canvas game object
canvasRoot = new GameObject( "Canvas" );
var canvas = canvasRoot.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
var cs = canvasRoot.AddComponent<CanvasScaler>();
cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
cs.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
cs.referenceResolution = new Vector2(800,600);
//create our ui prefab
uiPrefab = new GameObject( "UItext" );
uiPrefab.transform.position = new Vector3( 1000, 10000 );
var txt = uiPrefab.AddComponent<Text>();
txt.font = Resources.GetBuiltinResource( typeof( Font ), "Arial.ttf" ) as Font;
txt.text = "Some text";
txt.horizontalOverflow = HorizontalWrapMode.Overflow;
txt.color = Color.white;
txt.resizeTextForBestFit = true;
// Make a recycle bin for it
var recycleBin = new TrashManRecycleBin()
{
prefab = uiPrefab
};
TrashMan.manageRecycleBin( recycleBin );
}
}
示例14: create
void create()
{
//creation de la fenetre
GameObject newCanvas = new GameObject("Canvas");
Canvas ca = newCanvas.AddComponent<Canvas>();
ca.renderMode = RenderMode.ScreenSpaceOverlay;
//ajout de ces composents
newCanvas.AddComponent<CanvasScaler>();
newCanvas.AddComponent<GraphicRaycaster>();
//ajout du panel
GameObject panel = new GameObject("Panel");
panel.AddComponent<CanvasRenderer>();
Image i = panel.AddComponent<Image>();
i.color = new Color(0, 0, 0, 0.4f);
//le panel appartient a la fenetre
panel.transform.SetParent(newCanvas.transform, false);
// position et taille de la barre de spell
panel.transform.position = new Vector3((Screen.width / 2), 1, 0);
panel.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width - (Screen.width / 7), Screen.height / 5);
//ajout des cases de spell pour chaque capacité du joueur
for (int ic = 0; ic < transform.GetComponents<capacity>().Length; ic++)
{
GameObject text = new GameObject(transform.GetComponents<capacity>()[ic].name);
text.AddComponent<Text>().name = transform.GetComponents<capacity>()[ic].name;
text.GetComponent<Text>().text = transform.GetComponents<capacity>()[ic].K.ToString();
text.GetComponent<Text>().font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
text.transform.SetParent(newCanvas.transform, false);
text.transform.position = new Vector3(((Screen.width - (Screen.width / 7)) / transform.GetComponents<capacity>().Length) * ic + (Screen.width / 7), 1, 0);
LC.Add(transform.GetComponents<capacity>()[ic]);
transform.GetComponents<capacity>()[ic].T = text.GetComponent<Text>();
}
}
示例15: createInstance
public static void createInstance()
{
GameObject tmpObj = GameObject.Find("PersistentObj");
if(tmpObj != null)
{
instance = tmpObj;
}
else
{
// Basic object shell.
instance = new GameObject("PersistentObj");
instance.transform.position = new Vector3(0,0,0);
// Persistent object components.
// (Other components may be attached from within other scripts once you obtain an instance via getInstance())
//instance.AddComponent<ServerComRequester>();
instance.AddComponent<SoundPlayerScript>();
DatastoreScript ds = instance.AddComponent<DatastoreScript>();
// Store versioning info in memory. (Primarily needed by the saving system to detect old or new save data).
int dayValue = 6;//23;
int monthValue = 1;//10;
int yearValue = 2015;//2014;
int versionForDay = 0;
ds.insertData("GameVersionData",new SaveVersioningInfo(dayValue,monthValue,yearValue,versionForDay,""));
// Make sure that the object is not destroyed when new scenes are loaded.
UnityEngine.MonoBehaviour.DontDestroyOnLoad(instance);
}
}