playMaker

Author Topic: Detecting a device's platform, aka Operating System, or OS, in most cases  (Read 1379 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile

I also listed both Stadia and Cloud Rendering as unsupported because no code is given by Unity but it would probably look very close to the one for the Switch test, baring the respective swap for the two other platforms.

I also let them in the Enum because if they're valid, or validated sometime in the future, it will not break the action's enumeration indexing nor screw with users' work; on the coding side, a mere update in the script will be needed by changing the Enum entries to make them look like all other fully supported platforms, ex: UNITY_STADIA_Unsupported changed to UNITY_STADIA, with the necessary change further down in the script too for the detection phase.

Now, both IsOnPlatformEvent and RuntimeApplication are very similar actions since IsOnPlatformEvent only works at runtime too in Playmaker, even for tests... but because of Unity's two ways of checking for all the available platforms, there are few differences that exist as we can see with the three cases above.

If we let the RuntimePlatform instructions inside the IsOnPlatformEvent script, the user will never see the difference though but it's certainly not strictly conforming to Platform directives.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
Regarding RuntimeApplication, it looks like Unity will continue to count iOS/iPhonePlayer/iPadOS as the same.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6320
    • View Profile
    • jinxtergames
Hi.
Its an exposed enum from unity :)

so if new ones are added it will automatically be added to the action as well.

I believe that the #if 'Platform'

will exclude what's within it when building the project.
« Last Edit: March 09, 2021, 05:19:31 PM by djaydino »
Ecosystem might not work at the moment
here are direct Github Links to the custom actions :
https://github.com/PlayMakerEcosystem
https://github.com/jeanfabre


Join the PlayMaker Discord Channel!

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
Right, if Unity adds UNITY_STADIA and UNITY_CLOUD_RENDERING, the action will be already ready to work as is.
On RuntimeApplication, if someone wants to target an iPad in particular, iPhone Player won't suffice to sort it out though and perhaps this is where IsOnPlatformEvent will remain useful.
I might be extrapolating here but I think Unity will not update these UNITY_THINGSTUFF that much in the near future.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6320
    • View Profile
    • jinxtergames
Hi.
Quote
Right, if Unity adds UNITY_STADIA and UNITY_CLOUD_RENDERING, the action will be already ready to work as is.
Probably not as it will not state _Unsupported so the action would need to be updated manual.

The RuntimeApplication should be updated automatically as it is a enum and they can add new ones.
Ecosystem might not work at the moment
here are direct Github Links to the custom actions :
https://github.com/PlayMakerEcosystem
https://github.com/jeanfabre


Join the PlayMaker Discord Channel!

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
Quote
Right, if Unity adds UNITY_STADIA and UNITY_CLOUD_RENDERING, the action will be already ready to work as is.
Probably not as it will not state _Unsupported so the action would need to be updated manual.

Hmm right, I forgot about me adding _Unsupported. Edit custom enum, change code. I'm doing so many things at once to pack up my app, my memory span is becoming as short as a goldfish's.
« Last Edit: March 10, 2021, 09:33:02 AM by Broken Stylus »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6320
    • View Profile
    • jinxtergames
hehe, i know the feeling :D
Ecosystem might not work at the moment
here are direct Github Links to the custom actions :
https://github.com/PlayMakerEcosystem
https://github.com/jeanfabre


Join the PlayMaker Discord Channel!

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
I went back to this, replaced my version with yours that combines IOS and IPHONE checks and for some reason the code remains blocked in this region until I actually simplify the if blocks by removing the log and return.
No event gets sent.
Therefore one should either stick with the version that separates both IOS and IPHONE regions, or use the condensed version of the combined block version as below until the mystery is solved.

Code: [Select]
#if UNITY_IPHONE || UNITY_IOS
if(_flag == PlatformDependentFlags.UNITY_IPHONE )  isOnIPhone = true;

if(_flag == PlatformDependentFlags.UNITY_IOS )  isOnIOS = true;
#endif

With this piece, the action proceeds normally.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
I also made a new Action called RuntimePlatformEvent.

Could anyone test this one too and tell me if I'm the only one not detecting the editor (Windows OSX or Linux)?
I do editor tests before doing device tests because the device tests are based on the platform chosen in Build Settings, as it happens for the other action where both the Editor and the destination platform will be both detected.
So if you check for Editor first, you can have Editor-specific behaviors. This worked with the action I was editing but I'm not having any luck with RuntimePlatform.
On OSX, building for iOS, the OSX Editor isn't found.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
I'm using a slightly modified version of DJ's script here.

RuntimePlatformEventAdvance.cs

Quote
"Same as Runtime Platform Event, but also saves the platform found in a FsmString value, sets a boolean if any platform is found, but can also send a failure event if none is found. Also sends a log to the console about the result."

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2021. All rights reserved.
// License: Attribution 4.0 International(CC BY 4.0)
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/
// Author : Djaydino / http://www.jinxtergames.com/
// Modified : Broken Stylus
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

[ActionCategory(ActionCategory.Application)]
[Tooltip("Same as Runtime Platform Event, but also saves the platform found in a FsmString value, sets a boolean if any platform is found, but can also send a failure event if none is found. Also sends a log to the console about the result.")]
public class RuntimePlatformEventAdvance : FsmStateAction
{
        [CompoundArray("Enum Switches", "Compare Enum Values", "Send")]
        [MatchFieldType("enumVariable")]
        [ObjectType(typeof(RuntimePlatform))]
        public FsmEnum[] compareTo;
        public FsmEvent[] sendEvent;
        [Tooltip("The platform returned. 'Unknown' if none found.")]
        [Title("Store platform")]
        public FsmString platform;
        [Tooltip("Boolean set to true when a targeted platform matches.")]
        public FsmBool platformFound;
        [Tooltip("Event sent if there is no match.")]
        [Title("None found event")]
        public FsmEvent failure;

        public override void Reset()
        {
            compareTo = new FsmEnum[1];
            sendEvent = new FsmEvent[1];
            platform = "";
            platformFound = false;
        }

        public override void OnEnter()
        {
            DoEnumSwitch();
            Finish();
        }

        public override void OnUpdate()
        {
            DoEnumSwitch();
        }

        private void DoEnumSwitch()
        {

            for (int i = 0; i < compareTo.Length; i++)
            {
                if (Equals(Application.platform, compareTo[i].Value))
                {
                    platform.Value = Application.platform.ToString();
                    //platform.Value = compareTo[i].ToString();
                    Debug.Log(" Detected platform:  " + platform.Value + " ");
                    platformFound.Value = true;
                    Fsm.Event(sendEvent[i]);
                    return;
                }
            }
            platform.Value = "Unknown";
            Debug.LogWarning(" Unknown platform! ");
            platformFound.Value = false;
            Fsm.Event(failure);
        }
    }
}

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 657
    • View Profile
The good part is that the iOS/iPhone compiling regions in the code will work properly in the action's script IsOnPlatformEvent.

Now the silly thing: if you look into places such as components that can run functions, send events and do other things, like in a button for example with its OnClick(), you notice the three modes:
 
  • Off
  • Editor and Runtime
  • Runtime

This implies Runtime and Editor are two different things in this context and can be mutually exclusive, as if you could not obtain any info on the editor if you're working at runtime. This might sound stupid but I'm wondering if Unity has inadvertently excluded any possible detection of the editor at runtime and not noticed this since (I suppose) most people would use the #if UNITY_EDITOR, for instance.