-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "CodeWriter.TMP-Image.Editor", | ||
"rootNamespace": "", | ||
"references": [ | ||
"CodeWriter.TMP-Image", | ||
"Unity.TextMeshPro" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": false, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using UnityEditor; | ||
using UnityEditor.UI; | ||
using UnityEngine; | ||
|
||
namespace CodeWriter.UI | ||
{ | ||
[CustomEditor(typeof(TMPImage), true)] | ||
[CanEditMultipleObjects] | ||
public class TmpImageEditor : GraphicEditor | ||
{ | ||
private SerializedProperty m_SpriteName; | ||
private SerializedProperty m_PreserveAspect; | ||
|
||
protected override void OnEnable() | ||
{ | ||
base.OnEnable(); | ||
|
||
m_SpriteName = serializedObject.FindProperty("m_SpriteName"); | ||
m_PreserveAspect = serializedObject.FindProperty("m_PreserveAspect"); | ||
|
||
SetShowNativeSize(true); | ||
} | ||
|
||
public override void OnInspectorGUI() | ||
{ | ||
serializedObject.Update(); | ||
|
||
EditorGUILayout.PropertyField(m_SpriteName); | ||
|
||
AppearanceControlsGUI(); | ||
RaycastControlsGUI(); | ||
MaskableControlsGUI(); | ||
EditorGUILayout.PropertyField(m_PreserveAspect); | ||
SetShowNativeSize(false); | ||
NativeSizeButtonGUI(); | ||
|
||
serializedObject.ApplyModifiedProperties(); | ||
} | ||
|
||
void SetShowNativeSize(bool instant) | ||
{ | ||
base.SetShowNativeSize(true, instant); | ||
} | ||
|
||
private static Rect Outer(TMPImage tmpImage) | ||
{ | ||
var outer = tmpImage.uvRect; | ||
outer.xMin *= tmpImage.rectTransform.rect.width; | ||
outer.xMax *= tmpImage.rectTransform.rect.width; | ||
outer.yMin *= tmpImage.rectTransform.rect.height; | ||
outer.yMax *= tmpImage.rectTransform.rect.height; | ||
return outer; | ||
} | ||
|
||
public override bool HasPreviewGUI() | ||
{ | ||
var tmpImage = target as TMPImage; | ||
if (tmpImage == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var outer = Outer(tmpImage); | ||
return outer.width > 0 && outer.height > 0; | ||
} | ||
|
||
public override void OnPreviewGUI(Rect rect, GUIStyle background) | ||
{ | ||
var tmpImage = target as TMPImage; | ||
if (tmpImage == null) | ||
{ | ||
return; | ||
} | ||
|
||
var tex = tmpImage.mainTexture; | ||
if (tex == null) | ||
{ | ||
return; | ||
} | ||
|
||
var outer = Outer(tmpImage); | ||
TMPImage_SpriteDrawUtilityProxy.DrawSprite(tex, rect, outer, tmpImage.uvRect, | ||
tmpImage.canvasRenderer.GetColor()); | ||
} | ||
|
||
public override string GetInfoString() | ||
{ | ||
var tmpImage = target as TMPImage; | ||
if (tmpImage == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var text = string.Format("TMPImage Size: {0}x{1}", | ||
Mathf.RoundToInt(Mathf.Abs(tmpImage.rectTransform.rect.width)), | ||
Mathf.RoundToInt(Mathf.Abs(tmpImage.rectTransform.rect.height))); | ||
|
||
return text; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 CodeWriter Packages | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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) | ||
|
||
## :thought_balloon: Motivation | ||
|
||
I like the way to use sprites in the [Text Mesh Pro](https://docs.unity3d.com/Packages/com.unity.ugui@2.0) 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 | ||
|
||
## :rocket: How to use? | ||
|
||
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 | ||
|
||
## :open_book: How to Install | ||
|
||
Library distributed as git package ([How to install package from git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html)) | ||
<br>Git URL: `https://github.com/codewriter-packages/TMP-Image.git` | ||
|
||
## :green_book: License | ||
|
||
TMP-Image is [MIT licensed](./LICENSE.md). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "CodeWriter.TMP-Image", | ||
"rootNamespace": "", | ||
"references": [ | ||
"Unity.TextMeshPro" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.