Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity set parent canvas

[MenuItem("GameObject/UI/Switch")]
static void Switch()
{
    //Create new GameObject
    GameObject go = new GameObject("switch");


    //Find Canvas in the Scene
    Canvas canvas = (Canvas)GameObject.FindObjectOfType(typeof(Canvas));

    //Get Canvas GameObject
    GameObject canvasGameObject = canvas.gameObject;

    //Make the new GameObject child of the Canvas
    go.transform.parent = canvasGameObject.transform;
    go.transform.localPosition = Vector3.zero;

    //Change the new GameObject Layer to UI
    go.layer = 5; //Or go.layer = canvasGameObject.layer;

    //Add Rect Transform to it
    go.AddComponent<RectTransform>();
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: window height in C# forms 
Csharp :: unity check if current scene is being unloaded 
Csharp :: Failed to generate swagger file. Error dotnet swagger tofile --serializeasv2 --output 
Csharp :: c# sftp 
Csharp :: Scrollable WPF ListBox 
Csharp :: change line color in c# 
Csharp :: mysqldump - date 
Csharp :: concatenation in c# 
Csharp :: c# method returns multiple values 
Csharp :: bytes size c# 
Csharp :: c# substring reverse 
Csharp :: C# get column of 2d array 
Csharp :: C# linq mselect 
Csharp :: c# split include separators 
Csharp :: jagged array to 2d array c# 
Csharp :: how to sign in with your unity id in unity hub 
Csharp :: check if two timespans intersect c# 
Csharp :: generate UUID id for my entities 
Csharp :: how to check type in c# 
Csharp :: int array to frequency dictionary c# 
Csharp :: give an alias in model .net 
Csharp :: how to make randomizer c# 
Csharp :: concatanate two lists in c# 
Csharp :: pick random point inside box collider unity 
Csharp :: windows forms webbrowser refresh 
Csharp :: unity how to make gamemanager instance 
Csharp :: unity sprite disappears when using transform.lookat 
Csharp :: letter to number converter c# 
Csharp :: c# temporary files 
Csharp :: c# read excel file columns using epplus 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =