Skip to content

Commit 999f96f

Browse files
committed
Initial commit
0 parents  commit 999f96f

21 files changed

+483
-0
lines changed

Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "CodeWriter.TMP-Image.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"CodeWriter.TMP-Image",
6+
"Unity.TextMeshPro"
7+
],
8+
"includePlatforms": [
9+
"Editor"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": false,
14+
"precompiledReferences": [],
15+
"autoReferenced": false,
16+
"defineConstraints": [],
17+
"versionDefines": [],
18+
"noEngineReferences": false
19+
}

Editor/CodeWriter.TMP-Image.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/TMPImageEditor.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using UnityEditor;
2+
using UnityEditor.UI;
3+
using UnityEngine;
4+
5+
namespace CodeWriter.UI
6+
{
7+
[CustomEditor(typeof(TMPImage), true)]
8+
[CanEditMultipleObjects]
9+
public class TmpImageEditor : GraphicEditor
10+
{
11+
private SerializedProperty m_SpriteName;
12+
private SerializedProperty m_PreserveAspect;
13+
14+
protected override void OnEnable()
15+
{
16+
base.OnEnable();
17+
18+
m_SpriteName = serializedObject.FindProperty("m_SpriteName");
19+
m_PreserveAspect = serializedObject.FindProperty("m_PreserveAspect");
20+
21+
SetShowNativeSize(true);
22+
}
23+
24+
public override void OnInspectorGUI()
25+
{
26+
serializedObject.Update();
27+
28+
EditorGUILayout.PropertyField(m_SpriteName);
29+
30+
AppearanceControlsGUI();
31+
RaycastControlsGUI();
32+
MaskableControlsGUI();
33+
EditorGUILayout.PropertyField(m_PreserveAspect);
34+
SetShowNativeSize(false);
35+
NativeSizeButtonGUI();
36+
37+
serializedObject.ApplyModifiedProperties();
38+
}
39+
40+
void SetShowNativeSize(bool instant)
41+
{
42+
base.SetShowNativeSize(true, instant);
43+
}
44+
45+
private static Rect Outer(TMPImage tmpImage)
46+
{
47+
var outer = tmpImage.uvRect;
48+
outer.xMin *= tmpImage.rectTransform.rect.width;
49+
outer.xMax *= tmpImage.rectTransform.rect.width;
50+
outer.yMin *= tmpImage.rectTransform.rect.height;
51+
outer.yMax *= tmpImage.rectTransform.rect.height;
52+
return outer;
53+
}
54+
55+
public override bool HasPreviewGUI()
56+
{
57+
var tmpImage = target as TMPImage;
58+
if (tmpImage == null)
59+
{
60+
return false;
61+
}
62+
63+
var outer = Outer(tmpImage);
64+
return outer.width > 0 && outer.height > 0;
65+
}
66+
67+
public override void OnPreviewGUI(Rect rect, GUIStyle background)
68+
{
69+
var tmpImage = target as TMPImage;
70+
if (tmpImage == null)
71+
{
72+
return;
73+
}
74+
75+
var tex = tmpImage.mainTexture;
76+
if (tex == null)
77+
{
78+
return;
79+
}
80+
81+
var outer = Outer(tmpImage);
82+
TMPImage_SpriteDrawUtilityProxy.DrawSprite(tex, rect, outer, tmpImage.uvRect,
83+
tmpImage.canvasRenderer.GetColor());
84+
}
85+
86+
public override string GetInfoString()
87+
{
88+
var tmpImage = target as TMPImage;
89+
if (tmpImage == null)
90+
{
91+
return string.Empty;
92+
}
93+
94+
var text = string.Format("TMPImage Size: {0}x{1}",
95+
Mathf.RoundToInt(Mathf.Abs(tmpImage.rectTransform.rect.width)),
96+
Mathf.RoundToInt(Mathf.Abs(tmpImage.rectTransform.rect.height)));
97+
98+
return text;
99+
}
100+
}
101+
}

Editor/TMPImageEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE.md

Lines changed: 21 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# TMP-Image [![Github license](https://img.shields.io/github/license/codewriter-packages/TMP-Image.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/TMP-Image?style=flat-square)
2+
3+
## :thought_balloon: Motivation
4+
5+
I like the way to use sprites in the [Text Mesh Pro](https://docs.unity3d.com/Packages/[email protected]) by their name (e.g. `<sprite name=Gold>`). It would be very nice to be able to draw exactly the same sprites from the same atlases directly in UGUI. So I made a `TMPImage` - replacement for `Image` that able to do it
6+
7+
## :rocket: How to use?
8+
9+
Just add a `TMP Image` component to the GameObject and specify the sprite name, and sprite will be fetched from Text Mesh Pro and displayed. Simple
10+
11+
## :open_book: How to Install
12+
13+
Library distributed as git package ([How to install package from git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html))
14+
<br>Git URL: `https://github.com/codewriter-packages/TMP-Image.git`
15+
16+
## :green_book: License
17+
18+
TMP-Image is [MIT licensed](./LICENSE.md).

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)