当前位置: 首页>>代码示例>>C#>>正文


C# GameObject.GetOrAddComponent方法代码示例

本文整理汇总了C#中UnityEngine.GameObject.GetOrAddComponent方法的典型用法代码示例。如果您正苦于以下问题:C# GameObject.GetOrAddComponent方法的具体用法?C# GameObject.GetOrAddComponent怎么用?C# GameObject.GetOrAddComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnityEngine.GameObject的用法示例。


在下文中一共展示了GameObject.GetOrAddComponent方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Get

 public static EventTriggerListener Get(GameObject go)
 {
     //EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
     //if (listener == null)
     //    listener = go.AddComponent<EventTriggerListener>();
     //EventTriggerListener listener = go.GetOrAddComponent<EventTriggerListener>();
     return go.GetOrAddComponent<EventTriggerListener>();
 }
开发者ID:bzyzhang,项目名称:UIFrameWork,代码行数:8,代码来源:EventTriggerListener.cs

示例2: CreateGameObject

		/// Convinience method for creating a GameObject with a set of colored cubes components attached.
		/**
		 * Adding a volume to a scene requires creating a GameObject and then attching the required Cubiquity components such a renderer and a
		 * collider. This method simply automates the process and also attaches the provided volume data.
		 * 
		 * \param data The volume data which should be attached to the construced volume.
		 * \param addRenderer Specifies whether a renderer component should be added so that the volume is displayed.
		 * \param addCollider Specifies whether a collider component should be added so that the volume can participate in collisions.
		 */
		public static GameObject CreateGameObject(ColoredCubesVolumeData data, bool addRenderer, bool addCollider)
		{
			// Create our main game object representing the volume.
			GameObject coloredCubesVolumeGameObject = new GameObject("Colored Cubes Volume");
			
			//Add the required volume component.
			ColoredCubesVolume coloredCubesVolume = coloredCubesVolumeGameObject.GetOrAddComponent<ColoredCubesVolume>();
			
			// Set the provided data.
			coloredCubesVolume.data = data;
			
			// Add the renderer and collider if desired.
			if(addRenderer) { coloredCubesVolumeGameObject.AddComponent<ColoredCubesVolumeRenderer>(); }
			if(addCollider) { coloredCubesVolumeGameObject.AddComponent<ColoredCubesVolumeCollider>(); }
			
			// Return the created object
			return coloredCubesVolumeGameObject;
		}
开发者ID:20082037,项目名称:Evolcraft-1.0,代码行数:27,代码来源:ColoredCubesVolume.cs

示例3: CreateBinkMaterial

        /// Give an existing GameObject an icBinkMaterial with a given local Bink file.
        public icBinkMaterial CreateBinkMaterial(string filePath, GameObject go)
        {
            if (ToolbeltManager.FirstInstance.useBink)
            {
            icBinkMaterial mat = go.GetComponent<icBinkMaterial>();

            if (mat == null)
                mat = go.AddComponent<icBinkMaterial>();

            BinkPlugin bp = go.GetOrAddComponent<BinkPlugin>();
            mat.LoadMovie(filePath, bp);

            return mat;
            }
            else {
            Debug.LogWarning("Tried to CreateBinkMaterial() when ToolbeltManager::useBink is false. Returning null. \"" + filePath + "\"");
            return null;
            }
        }
开发者ID:rlawther,项目名称:AmnesiaMuseumUnity,代码行数:20,代码来源:icMediaLoader.cs

示例4: CreateHierarchy

		public void CreateHierarchy() {
			#if UNITY_EDITOR
			foreach (AudioClip clip in clips) {
				string clipPath = UnityEditor.AssetDatabase.GetAssetPath(clip).GetRange("Assets/Resources/".Length);
				string clipDirectory = Path.GetDirectoryName(clipPath);
				GameObject parent = GetOrAddFolder(clipDirectory);
				GameObject child = GameObject.Find(clip.name);
				
				if (child == null) {
					child = new GameObject(clip.name);
					PureDataSetup setup = child.GetOrAddComponent<PureDataSetup>();
					
					setup.name = clip.name;
					setup.Info = pureData.infoManager.GetInfo(clip.name);
					setup.pureData = pureData;
					setup.FreezeTransform();
				}
				child.transform.parent = parent.transform;
				child.transform.Reset();
			}
			#endif
		}
开发者ID:Kartoshka,项目名称:Crabby-Pulse-2,代码行数:22,代码来源:PureDataHierarchyManager.cs

示例5: syncNode

            public static void syncNode(ref uint availableSyncOperations, GameObject nodeGameObject, uint nodeHandle, GameObject voxelTerrainGameObject)
			{
                OctreeNode octreeNode = nodeGameObject.GetComponent<OctreeNode>();
                CuOctreeNode cuOctreeNode = CubiquityDLL.GetOctreeNode(nodeHandle);

                ////////////////////////////////////////////////////////////////////////////////
                // Has anything in this node or its children changed? If so, we may need to syncronise the node's properties, mesh and
                // structure. Each of these can be tested against a timestamp. We may also need to do this recursively on child nodes.
                ////////////////////////////////////////////////////////////////////////////////
                if (cuOctreeNode.nodeOrChildrenLastChanged > octreeNode.nodeAndChildrenLastSynced)
                {
                    bool resyncedProperties = false; // See comments where this is tested - it's a bit of a hack

                    ////////////////////////////////////////////////////////////////////////////////
                    // 1st test - Have the properties of the node changed?
                    ////////////////////////////////////////////////////////////////////////////////
                    if (cuOctreeNode.propertiesLastChanged > octreeNode.propertiesLastSynced)
                    {
                        octreeNode.renderThisNode = cuOctreeNode.renderThisNode != 0;
                        octreeNode.height = cuOctreeNode.height;
                        octreeNode.propertiesLastSynced = CubiquityDLL.GetCurrentTime();
                        resyncedProperties = true;
                    }

                    ////////////////////////////////////////////////////////////////////////////////
                    // 2nd test - Has the mesh changed and do we have time to syncronise it?
                    ////////////////////////////////////////////////////////////////////////////////
                    if ((cuOctreeNode.meshLastChanged > octreeNode.meshLastSynced) && (availableSyncOperations > 0))
                    {
                        if (cuOctreeNode.hasMesh == 1)
                        {
                            // Set up the rendering mesh											
                            VolumeRenderer volumeRenderer = voxelTerrainGameObject.GetComponent<VolumeRenderer>();
                            if (volumeRenderer != null)
                            {
                                MeshFilter meshFilter = nodeGameObject.GetOrAddComponent<MeshFilter>() as MeshFilter;
                                if(meshFilter.sharedMesh == null)
                                {
                                    meshFilter.sharedMesh = new Mesh();
                                }
                                MeshRenderer meshRenderer = nodeGameObject.GetOrAddComponent<MeshRenderer>() as MeshRenderer;

                                if (voxelTerrainGameObject.GetComponent<Volume>().GetType() == typeof(TerrainVolume))
                                {
                                    MeshConversion.BuildMeshFromNodeHandleForTerrainVolume(meshFilter.sharedMesh, nodeHandle, false);
                                }
                                else if (voxelTerrainGameObject.GetComponent<Volume>().GetType() == typeof(ColoredCubesVolume))
                                {
                                    MeshConversion.BuildMeshFromNodeHandleForColoredCubesVolume(meshFilter.sharedMesh, nodeHandle, false);
                                }

                                meshRenderer.enabled = volumeRenderer.enabled && octreeNode.renderThisNode;

                                // For syncing materials, shadow properties, etc.
                                syncNodeWithVolumeRenderer(nodeGameObject, volumeRenderer, false);
                            }

                            // Set up the collision mesh
                            VolumeCollider volumeCollider = voxelTerrainGameObject.GetComponent<VolumeCollider>();
                            if (volumeCollider != null)
                            {
                                bool useCollider = volumeCollider.useInEditMode || Application.isPlaying;

                                if (useCollider)
                                {
                                    // I'm not quite comfortable with this. For some reason we have to create this new mesh, fill it,
                                    // and set it as the collider's shared mesh, whereas I would rather just pass the collider's sharedMesh
                                    // straight to the functon that fills it. For some reason that doesn't work properly, and we see
                                    // issues with objects falling through terrain or not updating when part of the terrain is deleted.
                                    // It's to be investigated further... perhaps we could try deleting and recreating the MeshCollider?
                                    // Still, the approach below seems to work properly.
                                    Mesh collisionMesh = new Mesh();
                                    if (voxelTerrainGameObject.GetComponent<Volume>().GetType() == typeof(TerrainVolume))
                                    {
                                        MeshConversion.BuildMeshFromNodeHandleForTerrainVolume(collisionMesh, nodeHandle, true);
                                    }
                                    else if (voxelTerrainGameObject.GetComponent<Volume>().GetType() == typeof(ColoredCubesVolume))
                                    {
                                        MeshConversion.BuildMeshFromNodeHandleForColoredCubesVolume(collisionMesh, nodeHandle, true);
                                    }

                                    MeshCollider meshCollider = nodeGameObject.GetOrAddComponent<MeshCollider>() as MeshCollider;
                                    meshCollider.sharedMesh = collisionMesh;
                                }
                            }
                        }
                        // If there is no mesh in Cubiquity then we make sure there isn't one in Unity.
                        else
                        {
                            MeshCollider meshCollider = nodeGameObject.GetComponent<MeshCollider>() as MeshCollider;
                            if (meshCollider)
                            {
                                Utility.DestroyOrDestroyImmediate(meshCollider);
                            }

                            MeshRenderer meshRenderer = nodeGameObject.GetComponent<MeshRenderer>() as MeshRenderer;
                            if (meshRenderer)
                            {
                                Utility.DestroyOrDestroyImmediate(meshRenderer);
                            }
//.........这里部分代码省略.........
开发者ID:20082037,项目名称:Evolcraft-1.0,代码行数:101,代码来源:OctreeNode.cs

示例6: GenerateSphereCollider

        void GenerateSphereCollider(GameObject boneCollider, Vector2[] points)
        {
            float l = float.MaxValue;
            float r = float.MinValue;
            float d = float.MaxValue;
            float u = float.MinValue;

            for (int i = 0; i < points.Length; i++) {
                if (points[i].x < l) {
                    l = points[i].x;
                }

                if (points[i].x > r) {
                    r = points[i].x;
                }

                if (points[i].y < d) {
                    d = points[i].y;
                }

                if (points[i].y > u) {
                    u = points[i].y;
                }
            }

            float radius = Mathf.Max(Mathf.Abs(l - r), Mathf.Abs(d - u)) / 2 * physicsOptions.colliderSize;
            if (radius >= 0.001F) {
                SphereCollider sphere = boneCollider.GetOrAddComponent<SphereCollider>();
                sphere.radius = radius;
                sphere.center = new Vector3(l + r, u + d, 0) / 2;
            }
        }
开发者ID:Dracir,项目名称:Tarata-tesseract,代码行数:32,代码来源:ModelPhysicsOptionsDrawer.cs

示例7: GeneratePolygonCollider2D


//.........这里部分代码省略.........
            while (true) {
                float bestScore = float.MaxValue;
                bestPoint = firstPoint;

                for (int i = remainingPoints.Count - 1; i >= 0; i--) {
                    Vector2 point = remainingPoints[i];
                    float distanceFromPreviousPoint = Vector2.Distance(point, path.Last());

                    if (distanceFromPreviousPoint < minDistance) {
                        remainingPoints.Remove(point);
                    }
                    else if (point.x > path.Last().x) {
                        float score = Vector2.Distance(point, path.Last()) * previousPointDistanceMultiplier;
                        score -= Vector2.Distance(point, centerPoint) * centerDistanceMultiplier;
                        score += path.Last().y - point.y * wrongDirectionMultiplier;

                        if (score < bestScore) {
                            bestScore = score;
                            bestPoint = point;
                        }
                    }
                }

                if (bestPoint == firstPoint) {
                    break;
                }

                path.Add(remainingPoints.Pop(bestPoint));
            }

            // Going down-right
            bestPoint = -firstPoint;

            while (true) {
                float bestScore = float.MaxValue;
                bestPoint = firstPoint;

                for (int i = remainingPoints.Count - 1; i >= 0; i--) {
                    Vector2 point = remainingPoints[i];
                    float distanceFromPreviousPoint = Vector2.Distance(point, path.Last());

                    if (distanceFromPreviousPoint < minDistance) {
                        remainingPoints.Remove(point);
                    }
                    else if (point.y < path.Last().y) {
                        float score = Vector2.Distance(point, path.Last()) * previousPointDistanceMultiplier;
                        score -= Vector2.Distance(point, centerPoint) * centerDistanceMultiplier;
                        score += path.Last().x - point.x * wrongDirectionMultiplier;
                        if (score < bestScore) {
                            bestScore = score;
                            bestPoint = point;
                        }
                    }
                }

                if (bestPoint == firstPoint) {
                    break;
                }

                path.Add(remainingPoints.Pop(bestPoint));
            }

            // Going left-down
            bestPoint = -firstPoint;

            while (true) {
                float bestScore = float.MaxValue;
                bestPoint = firstPoint;

                for (int i = remainingPoints.Count - 1; i >= 0; i--) {
                    Vector2 point = remainingPoints[i];
                    float distanceFromPreviousPoint = Vector2.Distance(point, path.Last());

                    if (distanceFromPreviousPoint < minDistance) {
                        remainingPoints.Remove(point);
                    }
                    else if (point.x < path.Last().x) {
                        float score = distanceFromPreviousPoint * previousPointDistanceMultiplier;
                        score -= Vector2.Distance(point, centerPoint) * centerDistanceMultiplier;
                        score += point.y - path.Last().y * wrongDirectionMultiplier;

                        if (score < bestScore) {
                            bestScore = score;
                            bestPoint = point;
                        }
                    }
                }

                if (bestPoint == firstPoint) {
                    break;
                }

                path.Add(remainingPoints.Pop(bestPoint));
            }

            if (path.Count > 2) {
                PolygonCollider2D polygon2D = boneCollider.GetOrAddComponent<PolygonCollider2D>();
                polygon2D.SetPath(0, path.ToArray());
            }
        }
开发者ID:Dracir,项目名称:Tarata-tesseract,代码行数:101,代码来源:ModelPhysicsOptionsDrawer.cs

示例8: GenerateCircleCollider2D

        void GenerateCircleCollider2D(GameObject boneCollider, Vector2[] points)
        {
            float l = float.MaxValue;
            float r = float.MinValue;
            float d = float.MaxValue;
            float u = float.MinValue;

            for (int i = 0; i < points.Length; i++) {
                if (points[i].x < l) {
                    l = points[i].x;
                }

                if (points[i].x > r) {
                    r = points[i].x;
                }

                if (points[i].y < d) {
                    d = points[i].y;
                }

                if (points[i].y > u) {
                    u = points[i].y;
                }
            }

            float radius = Mathf.Max(Mathf.Abs(l - r), Mathf.Abs(d - u)) / 2 * physicsOptions.colliderSize;
            if (radius >= 0.001F) {
                CircleCollider2D circle2D = boneCollider.GetOrAddComponent<CircleCollider2D>();
                circle2D.radius = radius;
                circle2D.offset = new Vector2(l + r, u + d) / 2;
            }
        }
开发者ID:Dracir,项目名称:Tarata-tesseract,代码行数:32,代码来源:ModelPhysicsOptionsDrawer.cs

示例9: GenerateBoxCollider2D

        void GenerateBoxCollider2D(GameObject boneCollider, Vector2[] points)
        {
            float l = float.MaxValue;
            float r = float.MinValue;
            float d = float.MaxValue;
            float u = float.MinValue;

            for (int i = 0; i < points.Length; i++) {
                if (points[i].x < l) {
                    l = points[i].x;
                }

                if (points[i].x > r) {
                    r = points[i].x;
                }

                if (points[i].y < d) {
                    d = points[i].y;
                }

                if (points[i].y > u) {
                    u = points[i].y;
                }
            }

            Vector2 size = new Vector2(Mathf.Abs(l - r), Mathf.Abs(d - u)) * physicsOptions.colliderSize;
            if (size.x >= 0.055F || size.y >= 0.055F) {
                BoxCollider2D box2D = boneCollider.GetOrAddComponent<BoxCollider2D>();
                box2D.size = new Vector2(Mathf.Max(size.x, 0.055F), Mathf.Max(size.y, 0.055F));
                box2D.offset = new Vector2(l + r, u + d) / 2;
            }
        }
开发者ID:Dracir,项目名称:Tarata-tesseract,代码行数:32,代码来源:ModelPhysicsOptionsDrawer.cs

示例10: GenerateBoxCollider

        void GenerateBoxCollider(GameObject boneCollider, Vector2[] points)
        {
            float l = float.MaxValue;
            float r = float.MinValue;
            float d = float.MaxValue;
            float u = float.MinValue;

            for (int i = 0; i < points.Length; i++) {
                if (points[i].x < l) {
                    l = points[i].x;
                }

                if (points[i].x > r) {
                    r = points[i].x;
                }

                if (points[i].y < d) {
                    d = points[i].y;
                }

                if (points[i].y > u) {
                    u = points[i].y;
                }
            }

            Vector3 size = new Vector3(Mathf.Abs(l - r), Mathf.Abs(d - u), 1) * physicsOptions.colliderSize;
            if (size.x >= 0.001F || size.y >= 0.001F) {
                BoxCollider box = boneCollider.GetOrAddComponent<BoxCollider>();
                box.size = new Vector2(Mathf.Max(size.x, 0.001F), Mathf.Max(size.y, 0.001F));
                box.center = new Vector2(l + r, u + d) / 2;
                box.sharedMaterial = physicsOptions.colliderMaterial;
            }
        }
开发者ID:Dracir,项目名称:Final-bablititi,代码行数:33,代码来源:ModelPhysicsOptionsDrawer.cs

示例11: SerializeGameObjectReference

        public static void SerializeGameObjectReference(Stream stream, GameObject value)
        {
            if (value == null)
            {
                Basic.WriteByte(stream, 0);
                return;
            }

            var prefabId = PrefabStorage.Current.GetIndex(value);
            if (prefabId != -1)
            {
                Basic.WriteByte(stream, 1);
                Write(stream, prefabId);
            }
            else
            {
                Basic.WriteByte(stream, 2);
                int refId = value.GetOrAddComponent<FSReference>().GetPersistentId();
                Write(stream, refId);
            }
        }
开发者ID:srndpty,项目名称:VFW,代码行数:21,代码来源:ReflectiveComponentSerializer.cs


注:本文中的UnityEngine.GameObject.GetOrAddComponent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。