hi.
Currently I am creating an editor. But there was one problem.
Now you can create a bullet object with editorwindow open in edit mode.
However, in play mode, the move class in the gameobject is changed to a null reference.
Why is that?
If the Bullet in the Move class is serialized, will not the data be reset?
bullet.cs
[Serializable]
public class Bullet : ScriptableObject{
public Vector2 pos;
public float angle;
public float speed;
public bool Active{get;set;}
public virtual void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
}
public virtual Vector2 Move(Vector2 _pos)
{
pos = _pos;
double rad = angle * 3.14 * 2;
pos += new Vector2(Time.deltaTime * speed * (float)Math.Cos(rad), Time.deltaTime * speed * (float)Math.Sin(rad));
return pos;
}
public virtual void OnGUI()
{
pos = EditorGUILayout.Vector2Field("Position", pos);
angle = EditorGUILayout.Slider("Angle", angle,0,1);
speed = EditorGUILayout.FloatField("Speed", speed);
}
}
[Serializable]
public class DirectBullet : Bullet
{
public override Vector2 Move(Vector2 _pos)
{
pos = base.Move(_pos);
return pos;
}
public override void OnGUI()
{
base.OnGUI();
}
public override void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
}
}
BulletEditorWindow.cs
[Serializable]
public class BulletEditorWindow : EditorWindow {
static BulletEditorWindow editorWindow;
public BULLET_TYPE bullet_type;
public EDIT_MODE edit_mode;
[SerializeField]
private Bullet bullet;
[SerializeField]
private GameObject gameObject;
[SerializeField]
private Move move;
[MenuItem("Tool/Shooting Editor")]
public static void Init()
{
editorWindow = GetWindow<BulletEditorWindow>();
editorWindow.Show();
editorWindow.minSize = new Vector2(300, 600);
}
private void OnGUI()
{
edit_mode = (EDIT_MODE)GUILayout.Toolbar((int)edit_mode, new string[] { "Create", "Modify" });
bullet_type = (BULLET_TYPE)EditorGUILayout.EnumPopup("Type", bullet_type);
switch (edit_mode)
{
case EDIT_MODE.CREATE:
switch (bullet_type)
{
case BULLET_TYPE.DIRECT:
if (!(bullet is DirectBullet)) bullet = CreateInstance<DirectBullet>();
if ((bullet is DirectBullet)) bullet.OnGUI();
break;
}
break;
}
}
private void OnEnable()
{
bullet_type = BULLET_TYPE.DIRECT;
edit_mode = EDIT_MODE.CREATE;
hideFlags = HideFlags.HideAndDontSave;
SceneView.onSceneGUIDelegate += OnScene;
}
private void OnScene(SceneView sceneview)
{
if (Event.current.type == EventType.mouseDown)
{
switch (edit_mode)
{
case EDIT_MODE.CREATE:
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Vector2 pos = ray.origin;
switch (bullet_type)
{
case BULLET_TYPE.DIRECT:
gameObject = Instantiate(Resources.Load("Prefabs/DirectBullet") as GameObject, pos, new Quaternion());
gameObject.GetComponent<Move>().bullet = bullet;
break;
}
break;
}
}
}
Move.cs
public class Move : MonoBehaviour {
[SerializeField]
public Bullet bullet;
private void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
}
}