I’m creating a custom particle system and want to know what’s best practice and what’s fastest.
Each instantitated prefab needs properties such as its colour, velocity, spin, scale, lifetimeleft etc.
Previously I’ve used a manager with multiple arrays- as prefabs are instantiated, they’re added to an prefabArray and at the same time properties are added to various property arrays, velocityArray etc…
The prefab and its properties can then be accessed from a index value.
prefabArray i , returns the prefab
spinArray i , returns that prefabs spin
The manager would then for loop through the arrays and make the objects spin, change colour etc…
I’m wondering if this way of doing things is faster than having a script on each prefab which stores that prefab’s properties, as storing the properties on a script on the prefab seems more intuitive and easier to understand, the way outlined feels a bit clunky and gets hard to read.
Ive got the following code which houses properties on each prefab but i think I’m missing a trick could using a class be useful?
Thanks
// Adds the particles when space is pressed
var cube : Transform;
var currentRotation : Vector3;
var currentScale : float;
var currentVelocity : Vector3;
var currentColour : Color;
var currentSpin : Vector3;
var currentTime : float;
function Start () {
}
function Update () {
currentRotation = Vector3(Random.Range(0,360.0), Random.Range(0,360.0), Random.Range(0,360.0));
currentScale = Random.value * 5;
currentColour = Color(Random.value, Random.value, Random.value);
currentVelocity = Vector3(Random.Range(-0.3, 0.3),Random.Range(-0.3, 0.3),Random.Range(-0.3, 0.3));
currentSpin = Vector3(Random.Range(-10.0,10.0), Random.Range(-10.0,10.0), Random.Range(-10.0,10.0));
currentTime = Random.Range(1,10);
if (Input.GetKey("space")){
var prefab = Instantiate(cube, this.transform.localPosition, Quaternion.identity);
var script : ScriptCube = prefab.GetComponent(ScriptCube);
script.Initialise(currentRotation, currentScale, currentColour, currentVelocity, currentSpin, currentTime);
}
}
and
#pragma strict
// updates the prefabs properties
var startTime : float;
var timeLeft : float;
var velocity : Vector3;
var colour : Color;
var scale : float;
var rotation : Vector3;
var distanceFromOrigin : float;
var spin : Vector3;
var managerObject : GameObject;
var managerScript : ScriptManager;
function Start ()
{
managerObject = GameObject.Find("Manager");
managerScript = managerObject.GetComponent(ScriptManager);
}
function Update ()
{
this.transform.localPosition += velocity;
this.transform.Rotate(spin);
timeLeft -= Time.deltaTime;
if (timeLeft <= 0)
Disable();
}
function Reverse()
{
velocity *= -1;
}
function Initialise( rot : Vector3, sca : float, col : Color, vel : Vector3, spi : Vector3, tim : float)
{
rotation = rot;
scale = sca;
colour = col;
velocity = vel;
timeLeft = tim;
spin = spi;
startTime = Time.time;
distanceFromOrigin = 0;
this.transform.localRotation = Quaternion.Euler(rotation);
this.transform.localScale = Vector3(scale, scale, scale);
this.renderer.material.color = colour; // costly, replace with a material bank...
}
function Disable()
{
this.gameObject.SetActive(false);
}