Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Oct 20, 2024
0 parents commit 999f96f
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Editor/CodeWriter.TMP-Image.Editor.asmdef
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
}
7 changes: 7 additions & 0 deletions Editor/CodeWriter.TMP-Image.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions Editor/TMPImageEditor.cs
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;
}
}
}
11 changes: 11 additions & 0 deletions Editor/TMPImageEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE.md
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.
7 changes: 7 additions & 0 deletions LICENSE.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions README.md
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).
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/CodeWriter.TMP-Image.asmdef
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
}
7 changes: 7 additions & 0 deletions Runtime/CodeWriter.TMP-Image.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 999f96f

Please sign in to comment.