Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor(typeof(DataStage))]
public class DataStageEditor : Editor {
public override void OnInspectorGUI()
{
// Get this Stage-Data
DataStage dataStage = target as DataStage;
// Find "Stage" Object in Hierarchy-Window
var stageObject = GameObject.Find("Stage");
// Get Objects with Stage-Components("Item", Enemy", etc) in "Stage" Object
var items = stageObject.GetComponentsInChildren<StageItem>();
var characters = stageObject.GetComponentsInChildren<StageCharacter>();
if(GUILayout.Button("Save")){
// Overwrite Item-Data (Scriptable Object)
dataStage.items = new DataItem[items.Length];
for(int i=0; i<items.Length; i++){
dataStage.items[i] = new DataItem();
dataStage.items[i].name = items[i].gameObject.name;
dataStage.items[i].localPosition = items[i].transform.localPosition;
dataStage.items[i].localRotation = items[i].transform.localRotation;
dataStage.items[i].localScale = items[i].transform.localScale;
// and more Item-Component unique params
}
// Overwrite Enemy-Data (Scriptable Object)
dataStage.characters = new DataCharacter[characters.Length];
for(int i=0; i<characters.Length; i++){
dataStage.characters[i] = new DataCharacter();
dataStage.characters[i].name = characters[i].gameObject.name;
dataStage.characters[i].localPosition = characters[i].transform.localPosition;
dataStage.characters[i].localRotation = characters[i].transform.localRotation;
dataStage.characters[i].localScale = characters[i].transform.localScale;
// and more Enemy-Component unique params
}
}
if(GUILayout.Button("Load")){
// Clear Stage-Objects in Hierarchy
for(int i=items.Length-1; i>=0; i--)
DestroyImmediate(items[i].gameObject);
for(int i=characters.Length-1; i>=0; i--)
DestroyImmediate(characters[i].gameObject);
// Instantiate Stage-Enemies
for(int i=0; i<dataStage.characters.Length; i++){
var obj = Instantiate(Resources.Load("StageObject/Character")) as GameObject;
obj.name = dataStage.characters[i].name;
obj.transform.SetParent(stageObject.transform);
obj.transform.localPosition = dataStage.characters[i].localPosition;
obj.transform.localRotation = dataStage.characters[i].localRotation;
obj.transform.localScale = dataStage.characters[i].localScale;
}
// Instantiate Stage-Items
for(int i=0; i<dataStage.items.Length; i++){
var obj = Instantiate(Resources.Load("StageObject/Item")) as GameObject;
obj.name = dataStage.items[i].name;
obj.transform.SetParent(stageObject.transform);
obj.transform.localPosition = dataStage.items[i].localPosition;
obj.transform.localRotation = dataStage.items[i].localRotation;
obj.transform.localScale = dataStage.items[i].localScale;
}
}
DrawDefaultInspector();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.