Question by
jintaenate · Jul 25, 2019 at 08:43 AM ·
scripting problemclassmethod
How to use only 1 method for 2 objects which different class type
I have two objects, the difference is that there are two other classes, and both classes are inherited from the object class. i made code which can create, delete, save or load. So both objects are created, deleted from world, saved or loaded to asset by method
this is for object a
void CreateObject()
{
PersistableObject instance = Instantiate(persistablePrefab);
Transform t = instance.transform;
t.localPosition = Random.insideUnitSphere * 5f;
t.localRotation = Random.rotation;
t.localScale = Vector3.one * Random.Range(0.1f, 1f);
persistablePrefabs.Add(instance);
}
and this is for object b
void CreateShape()
{
ShapeOfPersistableObject instance = shapeFactory.GetRandom();
Transform t = instance.transform;
t.localPosition = Random.insideUnitSphere * 5f;
t.localRotation = Random.rotation;
t.localScale = Vector3.one * Random.Range(0.1f, 1f);
varietyShapePrefabs.Add(instance);
}
As you can see that both methods very similar, only different thing are list and class type
my question is how can i use only 1 method for 2 different class type objects
this is my full script
public class PersistableObjectsManager : PersistableObject
{
public KeyCode createKey = KeyCode.C;
public KeyCode newGameKey = KeyCode.N;
public KeyCode saveKey = KeyCode.S;
public KeyCode loadKey = KeyCode.L;
public PersistentStorage storage;
public enum ScriptType
{
PersistableObject, ShapeVariety
}
public ScriptType scriptType;
[Header("Persistable Object")]
public PersistableObject persistablePrefab;
List<PersistableObject> persistablePrefabs;
[Header("Shape Variety")]
public ShapeFactory shapeFactory;
List<ShapeOfPersistableObject> varietyShapePrefabs;
void Awake()
{
persistablePrefabs = new List<PersistableObject>();
varietyShapePrefabs = new List<ShapeOfPersistableObject>();
}
void Update()
{
switch (scriptType)
{
case ScriptType.PersistableObject:
InputMethodForPersistableObject();
break;
case ScriptType.ShapeVariety:
InputMethodForObjectsVariety();
break;
default:
break;
}
}
#region object
private void InputMethodForPersistableObject()
{
if (Input.GetKeyDown(createKey))
{
CreateObject();
}
else if (Input.GetKeyDown(newGameKey))
{
BeginNewGameForObject();
}
else if (Input.GetKeyDown(saveKey))
{
storage.Save(this);
}
else if (Input.GetKeyDown(loadKey))
{
BeginNewGameForObject();
storage.Load(this);
}
}
void CreateObject()
{
PersistableObject instance = Instantiate(persistablePrefab);
Transform t = instance.transform;
t.localPosition = Random.insideUnitSphere * 5f;
t.localRotation = Random.rotation;
t.localScale = Vector3.one * Random.Range(0.1f, 1f);
persistablePrefabs.Add(instance);
}
void BeginNewGameForObject()
{
for (int i = 0; i < persistablePrefabs.Count; i++)
{
Destroy(persistablePrefabs[i].gameObject);
}
persistablePrefabs.Clear();
}
#endregion
#region Shape
private void InputMethodForObjectsVariety()
{
if (Input.GetKeyDown(createKey))
{
CreateShape();
}
else if (Input.GetKeyDown(newGameKey))
{
BeginNewGameForShape();
}
else if (Input.GetKeyDown(saveKey))
{
storage.Save(this);
}
else if (Input.GetKeyDown(loadKey))
{
BeginNewGameForShape();
storage.Load(this);
}
}
void CreateShape()
{
ShapeOfPersistableObject instance = shapeFactory.GetRandom();
Transform t = instance.transform;
t.localPosition = Random.insideUnitSphere * 5f;
t.localRotation = Random.rotation;
t.localScale = Vector3.one * Random.Range(0.1f, 1f);
varietyShapePrefabs.Add(instance);
}
void BeginNewGameForShape()
{
for (int i = 0; i < varietyShapePrefabs.Count; i++)
{
Destroy(varietyShapePrefabs[i].gameObject);
}
varietyShapePrefabs.Clear();
}
#endregion
public override void Save(GameDataWriter writer)
{
switch (scriptType)
{
case ScriptType.PersistableObject:
//before save, need to open file via File.Open
writer.Write(persistablePrefabs.Count);
for (int i = 0; i < persistablePrefabs.Count; i++)
{
persistablePrefabs[i].Save(writer);
}
break;
case ScriptType.ShapeVariety:
//before save, need to open file via File.Open
writer.Write(varietyShapePrefabs.Count);
for (int i = 0; i < varietyShapePrefabs.Count; i++)
{
varietyShapePrefabs[i].Save(writer);
}
break;
default:
break;
}
}
public override void Load(GameDataReader reader)
{
int count = reader.ReadInt();
switch (scriptType)
{
case ScriptType.PersistableObject:
for (int i = 0; i < count; i++)
{
//PersistableObject @object = Instantiate(persistablePrefab);
PersistableObject instance = shapeFactory.Get(0);
instance.Load(reader);
persistablePrefabs.Add(instance);
}
break;
case ScriptType.ShapeVariety:
for (int i = 0; i < count; i++)
{
//PersistableObject @object = Instantiate(persistablePrefab);
ShapeOfPersistableObject instance = shapeFactory.Get(0);
instance.Load(reader);
varietyShapePrefabs.Add(instance);
}
break;
default:
break;
}
}
}
Please help me to figure out. thank you
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612205502im_/https://answers.unity.com/themes/thub/images/avi.jpg)