void OnGUI() { selectedTemplate = (GameObject)EditorGUILayout.ObjectField("Template :", selectedTemplate, typeof(GameObject), true); if (GUILayout.Button("Choose")) { if (selectedTemplate != null) { building.AddTemplate(selectedTemplate); } Close(); return; } }
/// <summary> /// Generate our templates for the building. /// </summary> /// <param name="name">The name of the template</param> /// <param name="building">what building is the template created for</param> /// <param name="templateTargets">the template objects you want to template</param> /// <param name="copy">Auto-Assign the template into the building</param> /// <returns>The generated template prefab</returns> public static GameObject GenerateTemplate(string name, BaseBuilding building, ITemplateObject[] templateTargets, bool copy) { Transform templateTransform; GameObject templateParent = new GameObject(name); templateParent.transform.position = building.transform.position; var templateScript = templateParent.AddComponent <Template>(); templateScript.templateObjects = templateTargets; templateScript.name = name; for (int i = 0; i < templateTargets.Length; i++) { if (copy) { templateTransform = templateTargets[i].GetTransform(); } else { templateTransform = ((GameObject)Instantiate(templateTargets[i].GetTransform().gameObject, templateTargets[i].GetTransform().position, templateTargets[i].GetTransform().rotation)).transform; templateTransform.name = templateTargets[i].GetTransform().name; } templateTransform.SetParent(templateParent.transform); } var prefab = PrefabUtility.CreatePrefab(PREFAB_PATH + name + ".prefab", templateParent, ReplacePrefabOptions.Default); if (copy) { building.AddTemplate(prefab); } DestroyImmediate(templateParent); AssetDatabase.SaveAssets(); return(prefab); }
static void UpdateBuilding(BaseBuilding building) { if (building.templates.Count > 0) // if we have templates { Template template; GameObject templatePrefab; GameObject buildingInstance = Instantiate <GameObject>(building.gameObject); BaseBuilding buildingComponent = buildingInstance.GetComponent <BaseBuilding>(); for (int i = 0; i < buildingComponent.templates.Count; i++) { template = buildingComponent.templates[i]; templatePrefab = Resources.Load <GameObject>(TemplateUtility.RESOURCES_PATH + template.name); buildingComponent.RemoveTemplate(template); buildingComponent.AddTemplate(templatePrefab); } PrefabUtility.ReplacePrefab(buildingInstance, building.gameObject, ReplacePrefabOptions.ReplaceNameBased); DestroyImmediate(buildingInstance); } }