Unity의 AddComponent에 대해 알아봤습니다.
12532 단어 Unity
MonoBehaviou 표시
Behaviour반->Component반->Unity Engine.Object 클래스가 상속되었습니다.
언뜻 보면 컴퍼니반을 물려받으면 Add 컴퍼니를 할 수 있을 것 같은데 틀렸어요.MonoBehaviour 클래스를 계승하지 않으면 AddComponent를 진행할 수 없습니다.
Unity에서 게임Object에 대한 AddComponent는 두 가지 방법이 있습니다.
1: Inspector의 AddCompoent Add 구성 요소에서
Inspector의 Add Component 버튼을 누르면 다음 작업이 수행됩니다.
if (AddComponentWindow.Show(rect, (
from o in firstNonImportInspectorEditor.targets
select (GameObject)o).ToArray<GameObject>()))
{
GUIUtility.ExitGUI();
}
Init 함수를 사용하여CreateComponentTree 함수 실행하기private void Init(Rect buttonRect)
{
buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
this.CreateComponentTree();
CreateComponentTree 함수를 사용하여 구성 요소 이름 가져오기 private void CreateComponentTree()
{
string[] submenus = Unsupported.GetSubmenus("Component");
string[] submenusCommands = Unsupported.GetSubmenusCommands("Component");
중요한 것은 CreateComponentTree 함수 내의 구성 요소 이름과 구성 요소 ID를 가져오는 것입니다.Unsupported.GetSubmenus 함수 결과
Unsupported.GetSubmenus 함수의 반환값(string[])
Component/Add...
Component/Mesh/Mesh Filter
Component/Mesh/Text Mesh
Component/Mesh/Mesh Renderer
Component/Mesh/Skinned Mesh Renderer
Component/Effects/Particle System
Component/Effects/Trail Renderer
Component/Effects/Line Renderer
Component/Effects/Lens Flare
Component/Effects/Halo
Component/Effects/Projector
Component/Effects/Legacy Particles/Ellipsoid Particle Emitter
Component/Effects/Legacy Particles/Mesh Particle Emitter
Component/Effects/Legacy Particles/Particle Animator
Component/Effects/Legacy Particles/World Particle Collider
Component/Effects/Legacy Particles/Particle Renderer
Component/Physics/Rigidbody
Component/Physics/Character Controller
Component/Physics/Box Collider
Component/Physics/Sphere Collider
Component/Physics/Capsule Collider
Component/Physics/Mesh Collider
Component/Physics/Wheel Collider
Component/Physics/Terrain Collider
Component/Physics/Cloth
Component/Physics/Hinge Joint
Component/Physics/Fixed Joint
Component/Physics/Spring Joint
Component/Physics/Character Joint
Component/Physics/Configurable Joint
Component/Physics/Constant Force
Component/Physics 2D/Rigidbody 2D
Component/Physics 2D/Box Collider 2D
Component/Physics 2D/Circle Collider 2D
Component/Physics 2D/Edge Collider 2D
Component/Physics 2D/Polygon Collider 2D
Component/Physics 2D/Distance Joint 2D
Component/Physics 2D/Fixed Joint 2D
Component/Physics 2D/Friction Joint 2D
Component/Physics 2D/Hinge Joint 2D
Component/Physics 2D/Relative Joint 2D
Component/Physics 2D/Slider Joint 2D
Component/Physics 2D/Spring Joint 2D
Component/Physics 2D/Target Joint 2D
Component/Physics 2D/Wheel Joint 2D
Component/Physics 2D/Area Effector 2D
Component/Physics 2D/Buoyancy Effector 2D
Component/Physics 2D/Point Effector 2D
Component/Physics 2D/Platform Effector 2D
Component/Physics 2D/Surface Effector 2D
Component/Physics 2D/Constant Force 2D
Component/Navigation/Nav Mesh Agent
Component/Navigation/Off Mesh Link
Component/Navigation/Nav Mesh Obstacle
Component/Audio/Audio Listener
Component/Audio/Audio Source
Component/Audio/Audio Reverb Zone
Component/Audio/Audio Low Pass Filter
Component/Audio/Audio High Pass Filter
Component/Audio/Audio Echo Filter
Component/Audio/Audio Distortion Filter
Component/Audio/Audio Reverb Filter
Component/Audio/Audio Chorus Filter
Component/Rendering/Camera
Component/Rendering/Skybox
Component/Rendering/Flare Layer
Component/Rendering/GUI Layer
Component/Rendering/Light
Component/Rendering/Light Probe Group
Component/Rendering/Reflection Probe
Component/Rendering/Occlusion Area
Component/Rendering/Occlusion Portal
Component/Rendering/LOD Group
Component/Rendering/Sprite Renderer
Component/Rendering/Canvas Renderer
Component/Rendering/GUI Texture
Component/Rendering/GUI Text
Component/Layout/Rect Transform
Component/Layout/Canvas
Component/Layout/Canvas Group
Component/Layout/Canvas Scaler
Component/Layout/Layout Element
Component/Layout/Content Size Fitter
Component/Layout/Aspect Ratio Fitter
Component/Layout/Horizontal Layout Group
Component/Layout/Vertical Layout Group
Component/Layout/Grid Layout Group
Component/Miscellaneous/Animator
Component/Miscellaneous/Animation
Component/Miscellaneous/Network View
Component/Miscellaneous/Wind Zone
Component/Miscellaneous/Terrain
Component/Miscellaneous/Billboard Renderer
Component/Event/Event System
Component/Event/Event Trigger
Component/Event/Physics 2D Raycaster
Component/Event/Physics Raycaster
Component/Event/Standalone Input Module
Component/Event/Touch Input Module
Component/Event/Graphic Raycaster
Component/Network/NetworkAnimator
Component/Network/NetworkDiscovery
Component/Network/NetworkIdentity
Component/Network/NetworkLobbyManager
Component/Network/NetworkLobbyPlayer
Component/Network/NetworkManager
Component/Network/NetworkManagerHUD
Component/Network/NetworkMigrationManager
Component/Network/NetworkProximityChecker
Component/Network/NetworkStartPosition
Component/Network/NetworkTransform
Component/Network/NetworkTransformChild
Component/Network/NetworkTransformVisualizer
Component/UI/Effects/Shadow
Component/UI/Effects/Outline
Component/UI/Effects/Position As UV1
Component/UI/Text
Component/UI/Image
Component/UI/Raw Image
Component/UI/Mask
Component/UI/2D Rect Mask
Component/UI/Button
Component/UI/Input Field
Component/UI/Toggle
Component/UI/Toggle Group
Component/UI/Slider
Component/UI/Scrollbar
Component/UI/Dropdown
Component/UI/Scroll Rect
Component/UI/Selectable
매개 변수반환값
"Component"
모든 구성 요소
"Component/UI"
Component/UI 아래의 구성 요소
"Hogehoge"
비어 있음(SubMenu 없음)
자신이 만든 각본은
Component/Scripts/
이하이다.이 때문에Unsupported.GetSubmenus("Component/Scripts")
를 지정하면 자체 스크립트만 가져올 수 있습니다.그나저나 서브메누는 특집
[AddComponentMenu ("階層/表示名")]
을 통해 지정할 수 있다.AddComponentMenu 추가
[AddComponentMenu ("Piyo/A")]
public class Test :MonoBehaviour
Unsupported.GetSubmenus Commands 함수 결과
Unsupported.GetSubmenus 함수의 반환값(string[])
ADD
33
102
23
137
198
96
120
123
122
119
~~~省略~~~...
SCRIPT1132
SCRIPT1094
SCRIPT1116
SCRIPT1102
SCRIPT1118
SCRIPT1072
SCRIPT1096
SCRIPT1134
SCRIPT1136
SCRIPT1128
SCRIPT1120
SCRIPT1078
SCRIPT1122
SCRIPT1124
SCRIPT1144
SCRIPT1170
SCRIPT9934
SCRIPT1252
여기에서 얻은 것은 구성 요소의 ID입니다.SCRIPT 숫자의 숫자 섹션을 사용하여 어셈블리의 축소판 이미지를 가져옵니다.4 매개변수의 commandString은 구성 요소 ID입니다.
public ComponentElement(int level, string name, string menuPath, string commandString)
{
this.level = level;
this.typeName = name.Replace(" ", string.Empty);
this.menuPath = menuPath;
if (commandString.StartsWith("SCRIPT"))
{
int instanceID = int.Parse(commandString.Substring(6));
UnityEngine.Object obj = EditorUtility.InstanceIDToObject(instanceID);
Texture miniThumbnail = AssetPreview.GetMiniThumbnail(obj);
this.content = new GUIContent(name, miniThumbnail);
}
else
{
int classID = int.Parse(commandString);
this.content = new GUIContent(name, AssetPreview.GetMiniTypeThumbnailFromClassID(classID));
}
}
Unsupported.GetSubmenus 및 UnsuportedGetSubmenus Commands 함수의 실제 처리는?
실제로 C++ 내부에는 숨겨져 있습니다.
Unsupported.cs
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string[] GetSubmenusCommands(string menuPath);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string[] GetSubmenus(string menuPath);
[MethodImpl(MethodImplOptions.InternalCall)] 원자 뷰어에서 일반 언어의 운행 시간에 처리를 의뢰합니다(C++에 처리 예정)[Wrapperless Icall] 아트라차르트, 응, 뭐지?Method Impl Options. Internal Call과 함께 사용하는 물건입니까?
I know WrapperlessIcall usually shows up with with MethodImpl in Unity codes, and they're about calling internal native code, but what is the purpose of WrapperlessIcall, exactly?
Like, Unity maybe use this attribute somewhere in their codes to distinguish the tagged methods, and maybe run automatic register process (mono_add_internal_call) for those tagged methods?
by marsonmao
If I remember correctly, they improved the managed to native call speed in Unity 3. It could be that they introduced a new attribute for that. But at the end it doesn't matter, because we can't use it anyways...
by Dantus
http://forum.unity3d.com/threads/unity-3-beta-wrapperlessicall.56378/
이 책을 읽으면 Method Impl Atrict는 C++와 연합할 수 있을 것 같지만 Wrapperless Icall Atrict가 있으면 C++의 방문이 빨라질 것 같다??
2: 스크립트에서 주소를 선택합니다.
void Start ()
{
var hoge = gameObject.AddComponent <Hoge> ();
}
그나저나 이렇게 Prefab에 AddComponent를 붙이면public class Test :MonoBehaviour
{
public GameObject prefab;
void Start ()
{
prefab.AddComponent <Test> ();
}
}
Prefab에서 Test 구성 요소는 게임 재생 중 게임을 처리하고 중지하는 경우에도 유지됩니다.GameObject의 AddComponent 함수를 검색한 후
AddComponent 함수
[TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
public Component AddComponent(Type componentType)
{
return this.Internal_AddComponentWithType(componentType);
}
public T AddComponent<T>() where T : Component
{
return this.AddComponent(typeof(T)) as T;
}
그렇습니다.Jernelic의 AddComponent 함수를 사용하여 Type을 매개 변수에 전달하는 AddComponent를 실행하고 있기 때문입니다
저쪽으로 좀 빨리 처리해 주세요.(오차 레벨일 수 있음)
AddComponent 함수의 속도를 측정해 보았습니다.
물건은 시도해야 한다
인증 코드
public class Test :MonoBehaviour
{
public GameObject prefab;
public GameObject[] instanceA;
public GameObject[] instanceB;
public int count = 1000;
void Start ()
{
instanceA = new GameObject[count];
instanceB = new GameObject[count];
for (int i = 0; i < count; i++) {
instanceA [i] = GameObject.Instantiate <GameObject> (prefab);
instanceB [i] = GameObject.Instantiate <GameObject> (prefab);
}
B ();
A ();
}
void A ()
{
Profiler.BeginSample ("Testdayo AAA");
for (int i = 0; i < instanceA.Length; i++) {
Hoge a = instanceA [i].AddComponent <Hoge> ();
}
Profiler.EndSample ();
}
void B ()
{
Profiler.BeginSample ("Testdayo BBB");
for (int i = 0; i < instanceB.Length; i++) {
Hoge a = instanceB [i].AddComponent (typeof(Hoge)) as Hoge;
}
Profiler.EndSample ();
}
}
결실20번 정도 해 보았지만 아마 AAA(AddComponent)가 느릴 거예요.
Reference
이 문제에 관하여(Unity의 AddComponent에 대해 알아봤습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ShirakawaMaru/items/c9621999965906396237텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)