Skip to content

Instantly share code, notes, and snippets.

@HarukaKajita
Created February 20, 2020 08:33
Show Gist options
  • Save HarukaKajita/601ee00aae6b078cc3ba4e50511ee2dc to your computer and use it in GitHub Desktop.
Save HarukaKajita/601ee00aae6b078cc3ba4e50511ee2dc to your computer and use it in GitHub Desktop.
複数のオブジェクトを特定のオブジェクトの方に向かせるエディタ拡張
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEditor;
using System.Linq;
public class LookAtWindow : EditorWindow
{
public Transform[] objects = null;
/////Angle
public Transform lookTarget = null;
public bool forwardFlip = false;
[MenuItem("Rage/LookAt")]
private static void Create()
{
GetWindow<LookAtWindow>("LookAt Window");
}
private void OnGUI(){
var MLW = new SerializedObject(this);
MLW.Update();
EditorGUILayout.PropertyField(MLW.FindProperty("objects"), true);
MLW.ApplyModifiedProperties();
lookTarget = (Transform) EditorGUILayout.ObjectField("Look Target", lookTarget, typeof(Transform), true);
forwardFlip = EditorGUILayout.Toggle("Forward Flip", forwardFlip);
if(GUILayout.Button("Look At")){
LookAtTarget();
}
}
void LookAtTarget(){
if(!lookTarget){
Debug.LogError("lookTarget is null.");
return;
}
if(objects == null){
Debug.LogError("objects are null.");
return;
}
foreach(var t in objects){
t.LookAt(lookTarget);
float rotX = t.localRotation.eulerAngles.x;
float rotZ = t.localRotation.eulerAngles.z;
t.localRotation = Quaternion.Euler(new Vector3(0, t.localRotation.eulerAngles.y+(forwardFlip?180:0), 0));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment