Question by
BuckyBall · Feb 25, 2019 at 02:35 PM ·
c#guieditorwindowaddcomponent
How to add Canvas Scaler and Graphic Raycaster to Canvas in code
I've created a custom EditorWindow that can create a new Canvas in code...
using UnityEngine;
using UnityEditor;
public class CreateFooCanvas : EditorWindow
{
private GameObject fooCanvas;
[MenuItem("Window/CreateFooCanvas")]
public static void ShowWindow()
{
GetWindow<CreateFooCanvas>();
}
void OnGUI()
{
// ...
if (GUILayout.Button("Create Canvas"))
{
fooCanvas = GameObject.Find("/FooCanvas");
if (fooCanvas != null)
{
Debug.Log(fooCanvas.name + "already exists");
}
else
{
fooCanvas = new GameObject("FooCanvas");
fooCanvas.AddComponent<RectTransform>();
fooCanvas.AddComponent<Canvas>();
fooCanvas.AddComponent<CanvasScaler>(); // Error
fooCanvas.AddComponent<GraphicRaycaster>(); // Error
}
}
}
}
The trouble is that the last two lines don't work - error: The type or namespace name 'GraphicRaycaster' could not be found.
How do I attach these two components to my new canvas?
Comment