There are lines for the execution of platform-specific code

#if UNITY_EDITOR && UNITY_STANDALONE_WIN #if UNITY_ANDROID 

How much do they load the system? Is it possible to put them in

 Update(){} 

Or how to write a platform dependent code for Update? So that under some conditions it is exactly what is needed?

    2 answers 2

    These are conditional compilation directives. They are carried out at assembly of the project, in them they do not exist.

    • I correctly understood that they can be put into the Update method - will they just run the necessary code and will not load the system? - Dmitrii
    • 2
      @Dmitrii when you start them at all. They will be deployed at compile time - pavel

    As @Pavel Mayorov wrote in response - all these constructions and unnecessary code after compilation will not. Those. if you write:

     #if UNITY_STANDALONE_WIN 10000 строк кода #endif #if UNITY_ANDROID 10000 строк кода #endif 

    Then after compilation you will have only those 10,000 lines that relate to the platform under which it was run.

    Or how to write a platform dependent code for Update? So that under some conditions it is exactly what is needed?

    In general, of course, depends on the complexity of the project. Roughly speaking, a project like Lineage 2, which Standalone has to do for Androyd too, I personally would not do it in one project right away. For you can break the brain and hands.

    But for small things, everything goes into the production of a common interaction interface and its implementation under the desired platform and its use.

    Example: We need to make the game Standalone and Android where it is possible

    • Click on the character (when clicking on him, he will say "What are you poking?")
    • With the key held down, you can move the character, he will scream "Aaa, help, save, I'm sick!"
    • You can release the button, the character says: "I will complain to the special authorities on you!"

    In the main game, the final class will look something like this:

     class ControlScript : MonoBehaviour { public IInputController inputController; void Start() { inputController = GetInputController(); } void Update() { if (inputController.IsTouchEnd()) Debug.Log("Пожалуюсь в спец органы на тебя!"); if (inputController.OnTouch().Count > 0) Debug.Log("Че тыкаешь?"); if (inputController.IsCursorMoving()) Debug.Log("А-а-а, помогите, спасите, меня тошнит!"); } private IInputController GetInputController() { IInputController controller = GetComponent<WinController>(); #if UNITY_STANDALONE_WIN IInputController controller = GetComponent<WinController>(); #endif #if UNITY_ANDROID IInputController controller = GetComponent<AndroidController>(); #endif return controller; } } 

    In it, at startup, we define the controller depending on the platform ( inputController = GetInputController() ), and in the Update method we use the methods thereof.


    The following components are presented: interface IInputController and classes that implement it.

    Interface for the controller, which will take the character:

     public interface IInputController { List<Vector3> OnTouch(); bool IsCursorMoving(); bool IsTouchEnd(); } 

    two classes that implement it (this is all conditional and is not a subject for imitation):

    WinController.cs

     public class WinController : MonoBehaviour, IInputController { public List<Vector3> OnTouch() { List<Vector3> list = new List<Vector3>(); if (Input.GetMouseButton(0)) list.Add(Input.mousePosition); return list; } public bool IsCursorMoving() { if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) return true; return false; } public bool IsTouchEnd() { if (Input.GetMouseButtonUp(0)) { return true; } return false; } } 

    AndroidController.cs

     public class AndroidController : MonoBehaviour, IInputController { private const int MAX_COUNT_TOUCHES = 2; public List<Vector3> OnTouch() { List<Vector3> list = new List<Vector3>(); for (var i = 0; i < Input.touchCount; ++i) { var phase = Input.GetTouch(i).phase; if (phase == TouchPhase.Stationary || phase == TouchPhase.Moved) { list.Add(Input.GetTouch(i).position); } if (list.Count >= MAX_COUNT_TOUCHES) break; } return list; } public bool IsCursorMoving() { if (Input.GetTouch(0).phase == TouchPhase.Moved) return true; return false; } public bool IsTouchEnd() { for (var i = 0; i < Input.touchCount; ++i) { var phase = Input.GetTouch(i).phase; if (phase == TouchPhase.Ended) return true; } return false; } } 

    Everything.

    HappyEnd.


    PS In addition to preprocessor directives, you can access the static variable platform of the Application class. It turns out something like this:

     RuntimePlatform platform = Application.platform; if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer) { // do smth } 

    link to class Application - https://docs.unity3d.com/en/current/ScriptReference/Application.html link to platforms - https://docs.unity3d.com/ru/current/ScriptReference/RuntimePlatform.html