Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by baribal · Feb 16, 2018 at 09:51 PM · c#style

Inline dot style methods/class chain

Hi ua, how to code in that style?

 LeanTween.rotateX(gameObject, 270.0f, 1.5f).setEase(LeanTweenType.easeInBack).setDelay(1.0f).setOnComplete(completeFunc);

It looks cool for me. Is this some kind of more advanced piece of coding? Any simple examples ? Steps? Keywords?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Feb 17, 2018 at 05:28 AM

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, ...)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 17, 2018 at 05:29 AM 2
Share

Hmm it got longer than i initially aimed for ^^.

avatar image ElijahShadbolt Bunny83 · Feb 17, 2018 at 05:40 AM 0
Share

Haha yep. Not gonna rewrite this answer any time soon.

avatar image baribal · Feb 17, 2018 at 06:15 PM 1
Share

@Bunny83 Thank you again for very detailed answer. I understand now. Even more with example of extending GameObject. It looks not so complicated than I thought. Awesome!

avatar image Harinezumi · Feb 19, 2018 at 08:29 AM 1
Share

The Definitive Style Guide by Bunny83 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

451 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Renderer on object disabled after level reload 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges