작은 컨트롤을 그리는 데 사용되는 기본 격자 가져오기 방법
12781 단어 UnityEditorUnityC#
거치다
Unity의 컨트롤러는 어떤 샘플을 제작하고 실체가 없는 Game Object의 위치를 파악하는 데 많이 사용된다.
기본적으로 다음 그림과 같은 작은 컨트롤 묘사를 지원합니다.

이 밖에 실린더와 캡슐을 사용하고 싶을 때의 간단한 샘플도 만들었다
완성물
어쨌든 먼저 만든 물건부터 보자
지원되는 것은 다음과 같습니다. (gif 그림을 난폭하게 허용합니다...)
기본체의 변환과 연동

기본체 모양 변경

솔리드 또는 와이어프레임에서 그리기 방법 전환하기

그릴 기본체의 색상 변경

스크립트
이번 스크립트는 샘플이기 때문에 편집기의 확장 코드도 한 파일에 저장됩니다.
신경 쓰이는 사람은 편집기용 코드와 그렇지 않은 것을 분리해서 정신 위생에 좋다.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace sample
{
public class Sample : MonoBehaviour
{
// 描画モード.
public enum DrawMode
{
Solid,
Wire,
}
[SerializeField] Color gizmoColor;
[SerializeField] DrawMode gizmoDrawMode;
[SerializeField] PrimitiveType usePrimitiveType = PrimitiveType.Cube;
[SerializeField] Mesh mesh = null;
void OnDrawGizmos()
{
if (mesh == null) return;
Gizmos.color = gizmoColor;
switch (gizmoDrawMode)
{
case DrawMode.Solid:
Gizmos.DrawMesh(mesh, transform.position, transform.rotation, transform.localScale);
break;
case DrawMode.Wire:
Gizmos.DrawWireMesh(mesh, transform.position, transform.rotation, transform.localScale);
break;
}
}
#if UNITY_EDITOR
// エディタ拡張部分.
[CustomEditor(typeof(Sample))]
public class SampleEditor : Editor
{
public override void OnInspectorGUI()
{
var component = target as Sample;
GUI.changed = false;
component.gizmoColor = EditorGUILayout.ColorField("Gizmo Color", component.gizmoColor);
component.gizmoDrawMode = (DrawMode)EditorGUILayout.EnumPopup("Draw Mode", component.gizmoDrawMode);
if (GUI.changed) EditorUtility.SetDirty(component);
// プリミティブタイプが変更された場合,メッシュを差し替える.
GUI.changed = false;
component.usePrimitiveType = (PrimitiveType)EditorGUILayout.EnumPopup(component.usePrimitiveType);
if (GUI.changed)
{
EditorUtility.SetDirty(component);
GameObject tempPrimitive = GameObject.CreatePrimitive(component.usePrimitiveType);
component.mesh = tempPrimitive.GetComponent<MeshFilter>().sharedMesh;
GameObject.DestroyImmediate(tempPrimitive);
}
// 取得したメッシュを確認できるようにしているだけ.
EditorGUILayout.ObjectField(component.mesh, typeof(Mesh));
}
}
#endif
}
}
간은 편집기 확장 부분의 ↓ 이 부분입니다. // プリミティブタイプが変更された場合,メッシュを差し替える.
GUI.changed = false;
component.usePrimitiveType = (PrimitiveType)EditorGUILayout.EnumPopup(component.usePrimitiveType);
if (GUI.changed)
{
EditorUtility.SetDirty(component);
GameObject tempPrimitive = GameObject.CreatePrimitive(component.usePrimitiveType);
component.mesh = tempPrimitive.GetComponent<MeshFilter>().sharedMesh;
GameObject.DestroyImmediate(tempPrimitive);
}
GUI 측 검사기에서 변경 사항이 있는지 확인합니다.- 베이스 객체 생성참조 페이지
- 생성된 객체에서 sharedMesh 읽어들이기
- 생성된 기본 객체 삭제
의 명세란 스타일에 정의된 설정입니다.
Reference
이 문제에 관하여(작은 컨트롤을 그리는 데 사용되는 기본 격자 가져오기 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/matsumotokaka11/items/6eda775cd82c2ef03e88텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)