Hey,
Just wanted to share my experience in great performance increase :
I’m actually reworking my code to optimize the hell out of it.
2 things I’m doing so far :
- grouping all my class datas into struct blocks.
For example, before I got :
public class MyClass : MonoBehaviour {
string _objectName;
int _objectNumber;
AnotherClass _objectClass;
}
Now I got :
public struct _objectStruct {
public string _name;
public int _number;
public AnotherClass _class;
}
public class MyClass : MonoBehaviour {
private _objectStruct _object;
}
Doing this for everything that could be grouped into a struct, separating different structs from their job. This helps to see things much clearer.
And according to MSDN, it is better performance (I’ll illustrate this later).
- replacing all my Classes instances by a single static Superclass.
Before I got :
public class PositionManager : MonoBehaviour {
//yadda yadda
}
public class ActionManager : MonoBehaviour {
//yadda yadda
}
public class MainClass : MonoBehaviour {
private ActionManager _actionManager;
private PositionManager _positionManager;
void Awake() {
_actionManager = (ActionManager) gameObject.GetComponent (typeof(ActionManager));
_positionManager = (positionManager) gameObject.GetComponent (typeof(positionManager));
}
void Main() {
// stuff using _actionManager _positionManager
}
}
public class OtherClass : MonoBehaviour {
private PositionManager _positionManager;
void Awake() {
_positionManager = (positionManager) gameObject.GetComponent (typeof(positionManager));
}
void Main() {
// stuff using _positionManager
}
}
And this kind of code was repeated as many times as there were characters on screen.
Now I got :
public class Superclass : MonoBehaviour {
static public PositionManager[] _positionManagers;
static public ActionManager[] _actionManagers;
static private string[] _charNames;
void Awake () {
int _numberOfCharacters = 2;
_charNames = new string[]{"Player1", "Player2"};
_positionManagers = new PositionManager[_numberOfCharacters];
_actionManagers = new ActionManager[_numberOfCharacters];
}
static public void Create_ActionManager(int _char) {
_actionManagers[_char] = GameObject.Find(_charNames[_char]).AddComponent(typeof(ActionManager)) as ActionManager;
}
static public ActionManager Get_ActionManager(int _char) {
return _actionManagers[_char];
}
static public void Create_PositionManager(int _char) {
_positionManagers[_char] = GameObject.Find(_charNames[_char]).AddComponent(typeof(PositionManager)) as PositionManager;
}
static public PositionManager Get_PositionManager(int _char) {
return _positionManagers[_char];
}
}
// skipping ActionManager PositionManager, they're the same as in "before" example
public class MainClass : MonoBehaviour {
void Main() {
int _char = _someCharNumber;
Superclass.Create_ActionManager(_char);
Superclass.Create_PositionManager(_char);
// stuff using Superclass.Get_ActionManager(_char) Superclass.Get_PositionManager(_char)
}
}
In short, I’m dramatically reducing the amount of instances in memory by setting all the storing and calling in a unique class, accessible from anywhere because it is static (= global).
I’m not done yet with the conversion of all my code, but all I can say is that before that struct / static conversion thing, I was hitting a frametime of 19ms to 23ms on a certain stage of my game (see attached picture).
Now, I’m hitting 17.2ms to 22ms.
(All tests on 3GS)
20ms is an average of 50 FPS.
17ms is 58 FPS.
That makes me very optimistic on final FPS, thanks to this kind of code architecture optimization.
