There is actually nothing special here. The order of actions in your example is like this:
- You call the static method “rotateX” in the LeanTween class.
- rotateX returns an instance of the LTDescr class.
- On the returned instance you then call setEase which is an instance method of the LTDescr class. The special property of that method is that it returns it’s own object which allows further chaining.
- On the return values of the setEase method you call the setDelay instance method. All those modifing instance methods simply return it’s own instance at the end.
Your example line would be the same as doing:
LTDescr obj = LeanTween.rotateX(gameObject, 270.0f, 1.5f);
LTDescr obj2 = obj.setEase(LeanTweenType.easeInBack);
LTDescr obj3 = obj2.setDelay(1.0f);
obj3.setOnComplete(completeFunc);
I used 3 seperate variables (obj, obj2, obj3) but in reality they all reference the same object that was created by the rotateX method. A more practical equivalent example would be:
LTDescr obj = LeanTween.rotateX(gameObject, 270.0f, 1.5f);
obj.setEase(LeanTweenType.easeInBack);
obj.setDelay(1.0f);
obj.setOnComplete(completeFunc);
So the 3 “set” methods here simply set / modify the state of the “LTDescr” object. Since each of them return their own object you can simply chain them. Simple chaining is quite common. Though it can easily become a mess. This is just normal plain C#:
string s = (123456.ToString().Substring(1, 4).Replace("3", "Hello").Replace("4", " World") + "! some more text").ToUpper();
the variable “s” will simply contain "2HELLO WORLD5! SOME MORE TEXT"
. To break this down you would have those individual steps:
string s = 123456.ToString(); // "123456"
s = s.Substring(1, 4); // "2345"
s = s.Replace("3", "Hello"); // "2Hello45"
s = s.Replace("4", " World"); // "2Hello World5"
s = s + "! some more text"; // "2Hello World5! some more text"
s = s.ToUpper(); // "2HELLO WORLD5! SOME MORE TEXT"
Note that this is exactly the same code. It will behave exactly the same. None is more efficient. The second version however is easier to follow and easier to debug in case there’s an error. Note that unlike in your example in this case each step produces a new string which is returned by the method / operator. Strings can’t be “mutated” or modified. So each of those methods will return a new string with the changes.
Though for example you can create extension methods for the GameObject class which always return the object they are called on to allow chaining. For example here are a few extension methods:
public static class GameObjectExt
{
public static GameObject ESetTag(this GameObject aObj, string aNewTag)
{
aObj.tag = aNewTag;
return aObj; // return own object to allow chaining
}
public static GameObject ESetName(this GameObject aObj, string aNewName)
{
aObj.name = aNewName;
return aObj; // return own object to allow chaining
}
public static GameObject ESetPosition(this GameObject aObj, Vector3 aPosition)
{
aObj.transform.position = aPosition;
return aObj; // return own object to allow chaining
}
public static GameObject ESetActive(this GameObject aObj, bool aActive)
{
aObj.SetActive(aActive)
return aObj; // return own object to allow chaining
}
}
With those you can do
GameObject go = new GameObject().ESetName("New object").ESetPosition(new Vector3(1, 2, 3)).ESetActive(false).ESetTag("Enemy");
The value that is assigned to “go” is actually the value returned by the last method (ESetTag). However since they all just return the incoming object it’s actually the object we just created. Though that’s not necessarily the case. For example we could also add a EClone method that creates a copy of the original object and returns the newly cloned object:
public static GameObject EClone(this GameObject aObj)
{
return Object.Instantiate(aObj);
}
With that we could do:
GameObject go = new GameObject("Enemy1").ESetPosition(new Vector3(0, 0, 0)).ESetTag("Enemy")
.EClone().ESetName("Enemy2").ESetPosition(new Vector3(3,0,0))
.EClone().ESetName("Enemy3").ESetPosition(new Vector3(6,0,0));
Note that this one line will actually create 3 gameobjects. “go” will receive the reference to the last clone (“Enemy3”). Also note that Enemy3 is actually a clone of Enemy2 and Enemy2 is a clone of the actual created gameobject at the beginning.
BUT
While this may look “cool” you should keep in mind the practical uses are quite rare. Also keep in mind that code should be readable and easy to understand in the first place. The last example hides the fact that it actually creates 3 seperate objects. You should generally avoid having too long lines. I actually put the “one line” on 3 lines. Note that spaces and new line characters are “mostly” ignored be the compiler. They can be placed almost anywhere. For example the line:
new GameObject("Enemy1").ESetPosition(new Vector3(0, 0, 0)).ESetTag("Enemy");
could be written as
new
GameObject (
"Enemy1"
) . ESetPosition(new
Vector3
(
0,
0,
0
) )
.
ESetTag
("Enemy")
;
which however doesn’t really increase the readability. Likewise we could write a whole class in a single line:
public class Test : MonoBehaviour{public int someInt;public string someStr;private void Start(){Debug.Log("str: " + someStr + " " + someInt);}}
This would be the same as this:
public class Test : MonoBehaviour
{
public int someInt;
public string someStr;
private void Start()
{
Debug.Log("str: " + someStr + " " + someInt);
}
}
I think it should be clear what’s the prefered way ^^. Of course there are a few things that require new line characters:
- line comments
// foobar
are terminated by the end of the current line. If you write everything in a single line you can’t use line comments (except at the very end). Though you can use block comments /* foobar */
in between
- most preprocessor directives require a new line ( so things like
#if
, #endif
, #region, …)