scriptableobject unity code example
Example 1: unity createassetmenu
using UnityEngine;
using System.Collections;
[CreateAssetMenu(fileName = "Data", menuName = "Inventory/List", order = 1)]
public class MyScriptableObjectClass : ScriptableObject {
public string objectName = "New MyScriptableObject";
public bool colorIsRandom = false;
public Color thisColor = Color.white;
public Vector3[] spawnPoints;
}
Example 2: unity scriptable objects
using UnityEngine;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class SpawnManagerScriptableObject : ScriptableObject
{
public string prefabName;
public int numberOfPrefabsToCreate;
public Vector3[] spawnPoints;
}
Example 3: unity scriptable object
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class SpawnManagerScriptableObject : ScriptableObject
{
public string prefabName;
public int numberOfPrefabsToCreate;
public Vector3[] spawnPoints;
}
Example 4: ScriptableObject
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject entityToSpawn;
public SpawnManagerScriptableObject spawnManagerValues;
int instanceNumber = 1;
void Start()
{
SpawnEntities();
}
void SpawnEntities()
{
int currentSpawnPointIndex = 0;
for (int i = 0; i < spawnManagerValues.numberOfPrefabsToCreate; i++)
{
GameObject currentEntity = Instantiate(entityToSpawn, spawnManagerValues.spawnPoints[currentSpawnPointIndex], Quaternion.identity);
currentEntity.name = spawnManagerValues.prefabName + instanceNumber;
currentSpawnPointIndex = (currentSpawnPointIndex + 1) % spawnManagerValues.spawnPoints.Length;
instanceNumber++;
}
}
}