Skip to content

Commit 40b4334

Browse files
committed
Initial package
1 parent 75b701e commit 40b4334

17 files changed

+1044
-1
lines changed

.editorconfig

+708
Large diffs are not rendered by default.

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Dd]ebug/
2+
[Rr]elease/
3+
x64/
4+
[Bb]in/
5+
[Oo]bj/
6+
7+
*.*~
8+
.vs
9+
.vscode
10+
.idea
11+
.gradle
12+
*.DotSettings.user

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 CodeWriter Packages
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# TutorialMaskForUGUI
1+
# TutorialMaskForUGUI [![Github license](https://img.shields.io/github/license/codewriter-packages/TutorialMaskForUGUI.svg?style=flat-square)](#) [![Unity 2021.3](https://img.shields.io/badge/Unity-2021.3+-2296F3.svg?style=flat-square)](#) ![GitHub package.json version](https://img.shields.io/github/package-json/v/codewriter-packages/TutorialMaskForUGUI?style=flat-square)
2+
23
UI Tutorial Mask is a component for highlighting specific objects over darkened background for Unity UI (uGUI)
4+
5+
![Demo](https://github.com/codewriter-packages/TutorialMaskForUGUI/assets/26966368/4b31beed-3324-42d8-ad7c-62edcafb1bdc)
6+
7+
## :open_book: How to Install
8+
Minimal Unity Version is 2021.3.
9+
10+
Library distributed as git package ([How to install package from git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html))
11+
<br>Git URL: `https://github.com/codewriter-packages/TutorialMaskForUGUI.git`
12+
13+
## :green_book: License
14+
15+
TutorialMaskForUGUI is [MIT licensed](./LICENSE.md).

README.md.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "CodeWriter.TutorialMaskForUGUI.Runtime"
3+
}

Runtime/CodeWriter.TutorialMaskForUGUI.Runtime.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/MaskFixForTutorial.cs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.EventSystems;
4+
using UnityEngine.Rendering;
5+
using UnityEngine.UI;
6+
7+
namespace CodeWriter.UIExtensions
8+
{
9+
[RequireComponent(typeof(Mask))]
10+
public class MaskFixForTutorial : UIBehaviour, IMaterialModifier
11+
{
12+
[NonSerialized]
13+
private Graphic _graphic;
14+
15+
[NonSerialized]
16+
private Mask _mask;
17+
18+
[NonSerialized]
19+
private Material _maskMaterial;
20+
21+
[NonSerialized]
22+
private Material _unmaskMaterial;
23+
24+
public Graphic Graphic => _graphic ?? (_graphic = GetComponent<Graphic>());
25+
public Mask Mask => _mask ?? (_mask = GetComponent<Mask>());
26+
27+
public Material GetModifiedMaterial(Material baseMaterial)
28+
{
29+
if (!Mask.MaskEnabled())
30+
{
31+
return baseMaterial;
32+
}
33+
34+
var rootSortCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
35+
var stencilDepth = MaskUtilities.GetStencilDepth(transform, rootSortCanvas);
36+
if (stencilDepth >= 7)
37+
{
38+
Debug.LogWarning("Attempting to use a stencil mask with depth > 8", gameObject);
39+
return baseMaterial;
40+
}
41+
42+
int desiredStencilBit = 1 << stencilDepth;
43+
44+
// if we are at the first level...
45+
// we want to destroy what is there
46+
if (desiredStencilBit == 1)
47+
{
48+
var maskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always,
49+
Mask.showMaskGraphic ? ColorWriteMask.All : 0, 255, desiredStencilBit);
50+
StencilMaterial.Remove(_maskMaterial);
51+
_maskMaterial = maskMaterial;
52+
53+
var unmaskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Zero, CompareFunction.Always, 0,
54+
255, desiredStencilBit);
55+
StencilMaterial.Remove(_unmaskMaterial);
56+
_unmaskMaterial = unmaskMaterial;
57+
Graphic.canvasRenderer.popMaterialCount = 1;
58+
Graphic.canvasRenderer.SetPopMaterial(_unmaskMaterial, 0);
59+
60+
return _maskMaterial;
61+
}
62+
63+
//otherwise we need to be a bit smarter and set some read / write masks
64+
var maskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit | (desiredStencilBit - 1),
65+
StencilOp.Replace, CompareFunction.Equal, Mask.showMaskGraphic ? ColorWriteMask.All : 0,
66+
desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));
67+
StencilMaterial.Remove(_maskMaterial);
68+
_maskMaterial = maskMaterial2;
69+
70+
Graphic.canvasRenderer.hasPopInstruction = true;
71+
var unmaskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Replace,
72+
CompareFunction.Equal, 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));
73+
StencilMaterial.Remove(_unmaskMaterial);
74+
_unmaskMaterial = unmaskMaterial2;
75+
Graphic.canvasRenderer.popMaterialCount = 1;
76+
Graphic.canvasRenderer.SetPopMaterial(_unmaskMaterial, 0);
77+
78+
return _maskMaterial;
79+
}
80+
}
81+
}

Runtime/MaskFixForTutorial.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/TutorialMask.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.EventSystems;
4+
using UnityEngine.Rendering;
5+
using UnityEngine.UI;
6+
7+
namespace CodeWriter.UIExtensions
8+
{
9+
[DisallowMultipleComponent]
10+
[RequireComponent(typeof(RectTransform))]
11+
[RequireComponent(typeof(Graphic))]
12+
[AddComponentMenu("CodeWriter/UIExtensions/Tutorial Mask")]
13+
public class TutorialMask : UIBehaviour, IMaterialModifier
14+
{
15+
[NonSerialized]
16+
private Material _maskMaterial;
17+
18+
protected override void OnDisable()
19+
{
20+
base.OnDisable();
21+
22+
StencilMaterial.Remove(_maskMaterial);
23+
_maskMaterial = null;
24+
}
25+
26+
public Material GetModifiedMaterial(Material baseMaterial)
27+
{
28+
if (!isActiveAndEnabled)
29+
{
30+
return baseMaterial;
31+
}
32+
33+
const int tutorialBit = 1 << 7;
34+
35+
var maskMat = StencilMaterial.Add(baseMaterial,
36+
stencilID: tutorialBit,
37+
operation: StencilOp.Keep,
38+
compareFunction: CompareFunction.NotEqual,
39+
colorWriteMask: ColorWriteMask.All,
40+
readMask: tutorialBit,
41+
writeMask: 0
42+
);
43+
StencilMaterial.Remove(_maskMaterial);
44+
_maskMaterial = maskMat;
45+
46+
return maskMat;
47+
}
48+
}
49+
}

Runtime/TutorialMask.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/TutorialObject.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.EventSystems;
4+
using UnityEngine.Rendering;
5+
using UnityEngine.UI;
6+
7+
namespace CodeWriter.UIExtensions
8+
{
9+
[DisallowMultipleComponent]
10+
[RequireComponent(typeof(RectTransform))]
11+
[RequireComponent(typeof(Graphic))]
12+
[AddComponentMenu("CodeWriter/UIExtensions/Tutorial Object")]
13+
public class TutorialObject : UIBehaviour, IMaterialModifier
14+
{
15+
[NonSerialized]
16+
private Graphic _graphic;
17+
18+
[NonSerialized]
19+
private Material _highlightMaterial;
20+
21+
public Graphic Graphic => _graphic ?? (_graphic = GetComponent<Graphic>());
22+
23+
protected override void OnEnable()
24+
{
25+
base.OnEnable();
26+
27+
if (Graphic != null)
28+
{
29+
Graphic.SetMaterialDirty();
30+
}
31+
}
32+
33+
protected override void OnDisable()
34+
{
35+
base.OnDisable();
36+
37+
if (Graphic != null)
38+
{
39+
Graphic.SetMaterialDirty();
40+
}
41+
42+
StencilMaterial.Remove(_highlightMaterial);
43+
_highlightMaterial = null;
44+
}
45+
46+
public Material GetModifiedMaterial(Material baseMaterial)
47+
{
48+
if (!IsActive())
49+
{
50+
return baseMaterial;
51+
}
52+
53+
const int tutorialBit = 1 << 7;
54+
55+
if (Graphic is MaskableGraphic maskableGraphic && maskableGraphic.maskable)
56+
{
57+
var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
58+
var stencilValue = MaskUtilities.GetStencilDepth(transform, rootCanvas);
59+
60+
if (stencilValue > 0)
61+
{
62+
var highlightMat = StencilMaterial.Add(baseMaterial,
63+
stencilID: tutorialBit | ((1 << stencilValue) - 1),
64+
operation: StencilOp.Replace,
65+
compareFunction: CompareFunction.Equal,
66+
colorWriteMask: ColorWriteMask.All,
67+
readMask: (1 << stencilValue) - 1,
68+
writeMask: tutorialBit
69+
);
70+
StencilMaterial.Remove(_highlightMaterial);
71+
_highlightMaterial = highlightMat;
72+
73+
return _highlightMaterial;
74+
}
75+
}
76+
77+
{
78+
var highlightMat = StencilMaterial.Add(baseMaterial,
79+
stencilID: tutorialBit,
80+
operation: StencilOp.Replace,
81+
compareFunction: CompareFunction.Always,
82+
colorWriteMask: ColorWriteMask.All,
83+
readMask: 0,
84+
writeMask: tutorialBit
85+
);
86+
StencilMaterial.Remove(_highlightMaterial);
87+
_highlightMaterial = highlightMat;
88+
}
89+
90+
return _highlightMaterial;
91+
}
92+
}
93+
}

Runtime/TutorialObject.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "com.codewriter.tutorial-mask",
3+
"displayName": "UI Tutorial Mask",
4+
"description": "UI Tutorial Mask is a component for highlighting specific objects over darkened background for Unity UI (uGUI)",
5+
"version": "1.0.0",
6+
"unity": "2021.3",
7+
"license": "MIT",
8+
"keywords": [
9+
"unity"
10+
],
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/codewriter-packages/TutorialMaskForUGUI.git"
14+
},
15+
"author": "CodeWriter (https://github.com/orgs/codewriter-packages)",
16+
"homepage": "https://github.com/codewriter-packages/TutorialMaskForUGUI#readme",
17+
"dependencies": {}
18+
}

package.json.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)