Skip to content

Commit 96cafec

Browse files
authored
Create CanvasRenderMode.cs
1 parent 7dd2abe commit 96cafec

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
// usage: Attach this script to gameobject with Canvas component
6+
// click mouse button to switch modes *note: worldspace will not be visible without scaling it
7+
// https://docs.unity3d.com/ScriptReference/Canvas-renderMode.html
8+
9+
namespace UnityLibrary
10+
{
11+
public class CanvasRenderMode : MonoBehaviour
12+
{
13+
Canvas canvas;
14+
15+
void Start()
16+
{
17+
canvas = GetComponent<Canvas>();
18+
if (canvas == null)
19+
{
20+
Debug.LogError("Canvas not found..", gameObject);
21+
this.enabled = false;
22+
}
23+
}
24+
25+
void Update()
26+
{
27+
// switch modes on left mouse click
28+
if (Input.GetMouseButtonDown(0))
29+
{
30+
if (canvas.renderMode != RenderMode.WorldSpace)
31+
{
32+
canvas.renderMode = RenderMode.WorldSpace;
33+
} else
34+
{
35+
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)