public void Initialize() { BindPrefabsFromFields(_prefabsMap); foreach (var contract in _contracts) { var key = contract.PrefabType; if (_prefabToLocationBindings.ContainsKey(key)) { Debug.Log("Multiple PrefabToLocationContracts for {0}" + contract.PrefabType.Name); } var binding = new PrefabToLocationBinding(contract); _prefabToLocationBindings[key] = binding; var location = GetLocation(contract.LocationProviderType); if (location == null) { continue; } var parent = contract.LocationIsAnchor ? location.parent : location; var anchor = contract.LocationIsAnchor ? location : null; var initiallyOnScene = parent == null ? GetComponentsOnSceneRootGameObjects(contract.PrefabType) : parent.transform.GetComponentsInFirstLayerChildren(contract.PrefabType); if (initiallyOnScene.Length > 1) { Debug.Log($"Multiple instances of {contract.PrefabType.Name} are not allowed."); } for (var i = 0; i != initiallyOnScene.Length; i++) { initiallyOnScene[i].gameObject.SetActive(false); } if (initiallyOnScene.Length <= 0) { continue; } if (anchor != null) { var instanceTransform = initiallyOnScene[0].transform; instanceTransform.position = anchor.position; instanceTransform.rotation = anchor.rotation; } binding.Bind(initiallyOnScene[0]); PrepareInstance(binding.Binded); } }
private T InstantiatePrefabByContract <T>(PrefabToLocationBinding binding) where T : Component { var contract = binding.Contract; var prefab = GetPrefab(contract.PrefabType); Debug.Log($"Instance of {contract.PrefabType.Name} created by contract"); var location = GetLocation(contract.LocationProviderType); if (location == null) { Debug.Log($"{contract.LocationProviderType.Name} binded location must be instantiated before {contract.PrefabType.Name}"); } var instance = InstantiatePrefab <T>(prefab, location, contract.LocationIsAnchor); binding.Bind(instance); return(instance); }