Ejemplo n.º 1
0
        /// <summary>
        /// Instantiates selected prefab in the unique relative path
        /// </summary>
        /// <param name="prefabId">Id of instantiated prefab in <see cref="NetworkSettings.DistributedObjectPrefabs"/></param>
        /// <param name="relativePath">Relative path to the root where object will be created, can be changed if object name is not unique</param>
        /// <returns>Instantiated new distributed object</returns>
        /// <exception cref="ArgumentException">Invalid configuration of the instantiated prefab</exception>
        private DistributedObject InstantiatePrefab(int prefabId, string relativePath)
        {
            if (prefabId < 0 || prefabId >= Settings.DistributedObjectPrefabs.Length)
            {
                throw new ArgumentException(
                          $"Prefab of distributed object with id {prefabId} is not defined in {typeof(NetworkSettings).Name}.");
            }
            var distributedObjectParent = HierarchyUtility.GetOrCreateChild(transform, relativePath);
            var newGameObject           = Instantiate(Settings.DistributedObjectPrefabs[prefabId], distributedObjectParent);

            HierarchyUtility.ChangeToUniqueName(newGameObject);
            var distributedObject = newGameObject.GetComponent <DistributedObject>();

            if (distributedObject == null)
            {
                throw new ArgumentException(
                          $"Prefab of distributed object with id {prefabId} has no {typeof(DistributedObject).Name} component in the root game object.");
            }
            instantiatedObjects.Add(new InstantiatedObjectData(prefabId, distributedObject));
            return(distributedObject);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Instantiates new prefab
        /// </summary>
        public void InstantiatePrefab(int prefabId, string relativePath, string objectName)
        {
            if (prefabId < 0 || prefabId >= Settings.DistributedObjectPrefabs.Length)
            {
                throw new ArgumentException(
                          $"Prefab of distributed object with id {prefabId} is not defined in {typeof(NetworkSettings).Name}.");
            }
            var rootTransform = transform;
            var newGameObject = Instantiate(Settings.DistributedObjectPrefabs[prefabId],
                                            rootTransform.position,
                                            rootTransform.rotation,
                                            HierarchyUtility.GetOrCreateChild(rootTransform, relativePath));

            newGameObject.name = objectName;
            var distributedObject = newGameObject.GetComponent <DistributedObject>();

            if (distributedObject == null)
            {
                throw new ArgumentException(
                          $"Prefab of distributed object with id {prefabId} has no {typeof(DistributedObject).Name} component in the root game object.");
            }
            instantiatedObjects.Add(new InstantiatedObjectData(prefabId, newGameObject));
        }