Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question about was is compiled and whats not

Discussion in 'Scripting' started by ricardoCoelho, Dec 17, 2020.

  1. ricardoCoelho

    ricardoCoelho

    Joined:
    Jul 6, 2020
    Posts:
    18
    I know that everything inside the Resources folder is allways compiled because its designed to be called at runtime, and everything outside of it, if it has no references, it is not compiled.
    But what about Unity build in functions? Specifically, UnityEngine.JsonUtility.
    I'd like to use Newtonsoft to parse my Jsons instead of Unity's built in parser and got curious if this would be compiled to my final build if I wasn't calling it anywhere.
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    One note is that you've mixed up assets vs code. All of the code in your project is always compiled, whether it's in Resources or not; the C# assembly is an interconnected web of code, and not compiling the entire assembly would be the cause of many potential errors.

    Assets are only included in a build if they are either referenced in a scene or in a Resources folder, which is what you're thinking of.

    After building, it is possible for Unity to analyze code and strip out some things that have no references. This is called managed code stripping and you can read about it here.

    One thing to consider about JsonUtility specifically is that it is really tiny. JsonUtility is basically just a JSON-flavored wrapper for Unity's built-in serializer, which is why it has all the same limitations (e.g. no Dictionaries). So whether or not JsonUtility is included in your build is going to be a difference of ...I'm not sure, but probably less than a kilobyte or something like that. Managed code stripping mostly matters for larger systems, such as the physics engines built in to Unity.
     
    Bunny83 and Joe-Censored like this.
  3. ricardoCoelho

    ricardoCoelho

    Joined:
    Jul 6, 2020
    Posts:
    18
    You are right, I was mixing concepts. Thanks for the heads up.
    This was mostly out of curiosity, not so much about the space it would ocupy in the final build. Since I'm going to replace it with a more robust serializer.
    I've read about how Unity strips code but this is still a bit advanced for me. Can you confirm that unity strips the JsonUtility and that I've highlighted the correct section mentioning that?


    upload_2020-12-17_17-16-59.png

    Also, what does it mean to 'Mark' something? That it will be removed or that whats marked won't be removed and everything else will?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The use of the word "Strip" there in the Action column is a bit misleading. It will only check if it thinks those things can be stripped. Basically if it sees you use it anywhere in your code, it won't be stripped. But your code isn't the only code in your project. For all I know Unity itself or some package you've included might use JsonUtility somewhere, and if so then it won't be stripped.
     
  5. ricardoCoelho

    ricardoCoelho

    Joined:
    Jul 6, 2020
    Posts:
    18
    @Joe-Censored I understand code can be used by Unity or some other DLL that I'm including in my project, but lets assume that it isn't being called anywhere, is it likely to be removed with a 'light code stripping configuration' (or with the other, more aggressive, configurations)?

    What if I have a situation like this:

    Code (CSharp):
    1. using UnityEngine
    2.  
    3. [Has 3 references]
    4. public class TestClass : Monobehaviour
    5. {
    6.    [Has 0 references]
    7.    public void DeserializeMessage( string message )
    8.    {
    9.        MyClass messageClass = JsonUtility.FromJson<MyClass>(message);
    10.        //Is this method removed? Because the class is being used to access the 'DoMath' method. And this is the only place JsonUtility is being used. Will it also be removed?
    11.    }
    12.    [Has 3 references]
    13.    public int DoMath( )
    14.    {
    15.        return 1 + 1;
    16.    }
    17. }
    Will JsonUtility be stripped from code since it is being called but the method calling it is not being called anywhere?
    Btw, I'm using JsonUtility as en example since its the one that peaked my curiosity. But we can talk about any other.
    This is just for me to grasp how code stripping works
     
    Last edited: Dec 18, 2020
  6. ricardoCoelho

    ricardoCoelho

    Joined:
    Jul 6, 2020
    Posts:
    18
    bumping thread