what's the best way of storing useful functions for future use?
sometimes unity does not have the perfect built-in function for certain situations, i have to write my own. I found myself repetitively using some of the functions I wrote before, I wonder what is a good way to store these functions and reuse them?
Answer by Landern · Nov 18, 2016 at 03:02 PM
You can create a Utility class for doing work with common methods if they are used a bunch why not make them static, this can apply to both unityscript and c#.
Example:
using System;
using UnityEngine;
public class Utilities
{
public static Vector3 CalculateMoveToPosition(Vector3 currentPos, Vector3 desiredPos)
{
return Vector3.Lerp(currentPos, desiredPos, Time.deltaTime);
}
// more methods
}
Then call it in code
//.. snip
go.Position = Utilities.CalculateMoveToPosition(go.Position, target.Position);
If you're not comfortable with static methods, you can create an instance of the Utilities class and call the methods just the same, perhaps have the reference on a game manager or some singleton.
Your answer
Follow this Question
Related Questions
Array of Arrays 3 Answers
Abnormal and Complex healthbar Shapes. Unity 5 2 Answers
Reuseable Dropdown 0 Answers
how to reuse key frame animation on beat' em up 2D games. 1 Answer