コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Transform の Rotation を Inspector における入力と同様に設定する方法

概要

localEulerAngles を使用する方法

using UnityEditor;
using UnityEngine;

public static class Example
{
    [MenuItem( "CONTEXT/Transform/Example" )]
    private static void Hoge( MenuCommand command )
    {
        var t = command.context as Transform;
        t.localEulerAngles = new Vector3( 270, 540, 810 );
    }
}

f:id:baba_s:20191125141642g:plain

localEulerAngles を使用して回転角を設定する場合、数値が自動で補正される

TransformUtils.SetInspectorRotation

using UnityEditor;
using UnityEngine;

public static class Example
{
    [MenuItem( "CONTEXT/Transform/Example" )]
    private static void Hoge( MenuCommand command )
    {
        var t = command.context as Transform;
        TransformUtils.SetInspectorRotation( t, new Vector3( 270, 540, 810 ) );
    }
}

f:id:baba_s:20191125141709g:plain

TransformUtils.SetInspectorRotationを使用する場合、数値をそのまま設定できる