以下の内容はhttps://baba-s.hatenablog.com/entry/2022/08/18/090000より取得しました。


【Unity】Device Simulator でデバイスが変更された時に呼び出されるイベント

ソースコード

using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.DeviceSimulation;
using UnityEngine;

namespace Kogane
{
    [InitializeOnLoad]
    public static class DeviceSimulatorDeviceChecker
    {
        private static readonly Type      SIMULATOR_WINDOW_TYPE       = typeof( SimulatorWindow );
        private static readonly FieldInfo DEVICE_SIMULATOR_MAIN_FIELD = SIMULATOR_WINDOW_TYPE.GetField( "m_Main", BindingFlags.Instance | BindingFlags.NonPublic );

        private static DeviceInfoAsset m_currentDevice;

        public static event Action OnChangeDevice;

        static DeviceSimulatorDeviceChecker()
        {
            EditorApplication.update += () => OnUpdate();
        }

        private static void OnUpdate()
        {
            if ( OnChangeDevice == null ) return;

            var simulatorWindow = Resources
                    .FindObjectsOfTypeAll<SimulatorWindow>()
                    .FirstOrDefault()
                ;

            if ( simulatorWindow == null ) return;

            var deviceSimulatorMain = ( DeviceSimulatorMain ) DEVICE_SIMULATOR_MAIN_FIELD.GetValue( simulatorWindow );
            var newDevice           = deviceSimulatorMain.currentDevice;

            if ( m_currentDevice != newDevice )
            {
                OnChangeDevice();
            }

            m_currentDevice = newDevice;
        }
    }
}

注意

Name が「UnityEditor.DeviceSimulation.Tests.Common」の
Assembly Definition を作成する必要があります




以上の内容はhttps://baba-s.hatenablog.com/entry/2022/08/18/090000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14