Tanoda
RaycastMask.cs
Go to the documentation of this file.
1
3
4/***************************************************************************\
5The MIT License (MIT)
6
7Copyright (c) 2014 Jonas Schiegl (https://github.com/senritsu)
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26\***************************************************************************/
27
28
30{
31 [RequireComponent(typeof(RectTransform))]
32 [RequireComponent(typeof(Image))]
33 [AddComponentMenu("UI/Extensions/Raycast Mask")]
34 public class RaycastMask : MonoBehaviour, ICanvasRaycastFilter
35 {
36 private Image _image;
37 private Sprite _sprite;
38
39 void Start()
40 {
41 _image = GetComponent<Image>();
42 }
43
44 public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
45 {
46 _sprite = _image.sprite;
47
48 var rectTransform = (RectTransform)transform;
49 Vector2 localPositionPivotRelative;
50 RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)transform, sp, eventCamera, out localPositionPivotRelative);
51
52 // convert to bottom-left origin coordinates
53 var localPosition = new Vector2(localPositionPivotRelative.x + rectTransform.pivot.x * rectTransform.rect.width,
54 localPositionPivotRelative.y + rectTransform.pivot.y * rectTransform.rect.height);
55
56 var spriteRect = _sprite.textureRect;
57 var maskRect = rectTransform.rect;
58
59 var x = 0;
60 var y = 0;
61 // convert to texture space
62 switch (_image.type)
63 {
64
65 case Image.Type.Sliced:
66 {
67 var border = _sprite.border;
68 // x slicing
69 if (localPosition.x < border.x)
70 {
71 x = Mathf.FloorToInt(spriteRect.x + localPosition.x);
72 }
73 else if (localPosition.x > maskRect.width - border.z)
74 {
75 x = Mathf.FloorToInt(spriteRect.x + spriteRect.width - (maskRect.width - localPosition.x));
76 }
77 else
78 {
79 x = Mathf.FloorToInt(spriteRect.x + border.x +
80 ((localPosition.x - border.x) /
81 (maskRect.width - border.x - border.z)) *
82 (spriteRect.width - border.x - border.z));
83 }
84 // y slicing
85 if (localPosition.y < border.y)
86 {
87 y = Mathf.FloorToInt(spriteRect.y + localPosition.y);
88 }
89 else if (localPosition.y > maskRect.height - border.w)
90 {
91 y = Mathf.FloorToInt(spriteRect.y + spriteRect.height - (maskRect.height - localPosition.y));
92 }
93 else
94 {
95 y = Mathf.FloorToInt(spriteRect.y + border.y +
96 ((localPosition.y - border.y) /
97 (maskRect.height - border.y - border.w)) *
98 (spriteRect.height - border.y - border.w));
99 }
100 }
101 break;
102 case Image.Type.Simple:
103 default:
104 {
105 // conversion to uniform UV space
106 x = Mathf.FloorToInt(spriteRect.x + spriteRect.width * localPosition.x / maskRect.width);
107 y = Mathf.FloorToInt(spriteRect.y + spriteRect.height * localPosition.y / maskRect.height);
108 }
109 break;
110 }
111
112 // destroy component if texture import settings are wrong
113 try
114 {
115 return _sprite.texture.GetPixel(x, y).a > 0;
116 }
117 catch (UnityException)
118 {
119 Debug.LogWarning("Mask texture not readable, set your sprite to Texture Type 'Advanced' and check 'Read/Write Enabled'");
120 Destroy(this);
121 return false;
122 }
123 }
124 }
125}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
Definition: RaycastMask.cs:44
Credit Erdener Gonenc - @PixelEnvision.