Search Unity

Where can I learn about how Unity runs ?

Discussion in 'Documentation' started by baemincheon, Dec 23, 2018.

  1. baemincheon

    baemincheon

    Joined:
    Nov 30, 2018
    Posts:
    6
    one day I've got a question, "In general CSharp program, it should have the main function, which is a start point of the program. So, where is the main function of Unity project ?"

    I search for several hours to find the answers, and got some info below:
    - Unity engine is written by CPP, not the CSharp
    - CSharp script is just a method to use Unity engine through API
    - we cannot usually access on deep side of Unity engine
    (especially referenced https://answers.unity.com/questions/9675/is-unity-engine-written-in-monoc-or-c.html)

    so I understood as "Actual main function is written by CPP and invisible"
    then, I have new 2 questions below:
    #1. there is no way to learn about how Unity runs ? like https://docs.unity3d.com/Manual/ExecutionOrder.html
    #2. how the CSharp script can execute CPP code (Unity engine) ? I heard dotnet makes it possible, but I cannot find an explanation for it

    sorry for bad english, I'll really appreciate for your answer !
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    My I ask, what you try to accomplish?
    There is Start, Update, FixUpdate etc C# methods. For most, that is.more than enough. If you wand Unity c++ source code, you may need purchase Unity Pro.
     
  3. baemincheon

    baemincheon

    Joined:
    Nov 30, 2018
    Posts:
    6
    thanks your reply :D

    I know which functions be executed at start of script and which functions should be used for specific purposes. what I wanna know is the process our CSharp scripts be handled in the engine level. (e.g. some module imports and compiles our scripts, other module performs functions of each script...)

    it seems subscribing Unity Pro would be the answer when the engine codes are provided. have you ever experienced the Unity Pro, or source code access ? I wonder how much it is supported
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Me nope. I don't have good expertise in C++. Also I am using Unity as hobby, not commercially at given time. Hence I don't need Pro.

    Unfortunately, for the rest of question, I am unable to advise. Someone else would be more suitable perhaps.
     
  5. baemincheon

    baemincheon

    Joined:
    Nov 30, 2018
    Posts:
    6
    okay thanks your advice, have a nice time
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It's not a C# program running c++ code. It's a c++ program that runs C# code.

    You can get access to Unity's internals by getting source access, but that's not a part of pro. You need an enterprise subscription, or some other deal for that. The cost is not listed, and I assume that it includes enough NDA's that somebody that has access can't really answer your questions.
     
    Antypodish likes this.
  7. baemincheon

    baemincheon

    Joined:
    Nov 30, 2018
    Posts:
    6
    Yes now I see that the engine handles our scripts, but problem is I cannot find something explains how it works.

    Code access seems too hard for me if it is needed as enterprise subscription and cost too. Then I think it is impossible to know how Unity engine runs, but at least, can I learn about how Unity handle our scripts ?

    I know CSharp compilation process makes some IL code as DLL or EXE format, and it maybe related to generation of "Project Folder/Library/ScriptAssemblies/Assembly-CSharp.dll" in my sight. And I know that the IL code is not compatible with CPP due to it is written by MSIL, not the CPP. Is the IL2CPP thing the key of this problem ? Few articles I found say like "Unity uses IL2CPP for handling scripts" ( https://blogs.unity3d.com/kr/2015/05/06/an-introduction-to-ilcpp-internals/ ). Could you recommend any article related to this subject ?
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    C# is a VM-based language. This means that the compiled code is run in a virtual machine, which itself is a program. If you're not opting in to IL2CPP, or you're running in the editor, Unity's running the Mono runtime as the VM. It's running as a part of the Unity runtime. I don't actually know what runtime's used when running with IL2CPP - if they've written their own VM or if they're using Mono - since the language is a garbage collected one, there has to be a VM around even if it's converted to c++.

    The C# user code and c++ engine code is communicating directly - from C# to c++ though extern calls, which you find a bunch of if you look at the publicly available C# source.

    "How unity handles our scripts" is a pretty large topic. Do you mean how they run the magic methods (ie. Update)? Or how something else works? This information isn't exactly well-documented, mostly because it's not very important to make stuff run well.
     
  9. baemincheon

    baemincheon

    Joined:
    Nov 30, 2018
    Posts:
    6
    It looks like not that simple. My origin thought was finding some flowchart or diagram explaining Unity script handling, like form of Graphics Pipeline ones. Actually, what I wondered is "How Unity engine executes CSharp scripts". Because Unity manual only says the order of methods, such as Start(), Update() and etc., not the way that the scripts are executed by the engine or what component of the engine manages the scripts.

    Anyway, as a beginner of Unity, it is not good to dive into too deep inside. First of all, I gotta learn about the things in this thread. Thanks your reply Baste ! :D
     
  10. SurajKamya

    SurajKamya

    Joined:
    Dec 16, 2021
    Posts:
    8