Skip to content

Commit 14e6a73

Browse files
author
TarasPC
committed
main. Original code by Thomas "noio" van den Berg.
- Installed localization package. - asmdef references.
1 parent 4ba52a1 commit 14e6a73

9 files changed

Lines changed: 202 additions & 4 deletions

Packages/com.starasgames.unity-clean-localized-string-inspector/Editor/CleanLocalizedStringInspector.Editor.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "CleanLocalizedStringInspector.Editor",
33
"rootNamespace": "CleanLocalizedStringInspector.Editor",
44
"references": [
5-
"CleanLocalizedStringInspector"
5+
"CleanLocalizedStringInspector",
6+
"Unity.Localization",
7+
"Unity.Localization.Editor"
68
],
79
"includePlatforms": [
810
"Editor"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using UnityEngine.Localization;
4+
using UnityEngine.Localization.Settings;
5+
using UnityEngine.Localization.Tables;
6+
7+
namespace Utils.SimpleLocalizedStringDrawer.Editor
8+
{
9+
[CustomPropertyDrawer(typeof(SimpleLocalizedStringAttribute))]
10+
public class SimpleLocalizedStringDrawer : PropertyDrawer
11+
{
12+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
13+
{
14+
var isLocaleSet = (LocalizationSettings.SelectedLocale != null);
15+
16+
if (property.isExpanded || isLocaleSet == false)
17+
{
18+
/*
19+
* JUST DRAW THE REGULAR LOCALIZED STRING
20+
*/
21+
EditorGUI.PropertyField(position, property, label, true);
22+
}
23+
else
24+
{
25+
/*
26+
* IF WE CAN FIND A TRANSLATION IN THE CURRENT LOCALE:
27+
* just draw a text field with that value.
28+
*
29+
*/
30+
EditorGUI.BeginProperty(position, label, property);
31+
32+
var attr = attribute as SimpleLocalizedStringAttribute;
33+
var fieldHeight = attr.Multiline ? EditorGUIUtility.singleLineHeight * 3 : EditorGUIUtility.singleLineHeight;
34+
35+
var foldoutRect = new Rect(position.x, position.y, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight);
36+
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true);
37+
38+
var textFieldRect = new Rect(position.x + EditorGUIUtility.labelWidth, position.y,
39+
position.width - EditorGUIUtility.labelWidth, fieldHeight);
40+
41+
var localizedString = (LocalizedString)property.boxedValue;
42+
43+
StringTable table = null;
44+
StringTableEntry entry = null;
45+
string value = "";
46+
if (localizedString.IsEmpty == false)
47+
{
48+
table = LocalizationSettings.StringDatabase.GetTable(localizedString.TableReference);
49+
entry = table?.GetEntryFromReference(localizedString.TableEntryReference);
50+
value = entry?.Value;
51+
}
52+
53+
EditorGUI.BeginChangeCheck();
54+
string newValue = attr.Multiline
55+
? EditorGUI.TextArea(textFieldRect, value)
56+
: EditorGUI.TextField(textFieldRect, value);
57+
if (EditorGUI.EndChangeCheck() && !string.IsNullOrEmpty(newValue))
58+
{
59+
if (entry != null)
60+
{
61+
entry.Value = newValue;
62+
EditorUtility.SetDirty(table);
63+
EditorUtility.SetDirty(table.SharedData);
64+
}
65+
else
66+
{
67+
table = LocalizationSettings.StringDatabase.GetTable(attr.TableName);
68+
var tempKey = $"TEMP_{System.Guid.NewGuid():N}";
69+
70+
entry = table.AddEntry(tempKey, newValue);
71+
Debug.Log($"Created Entry in Table {table}: <b>{tempKey}</b> (will be renamed on save)");
72+
73+
EditorUtility.SetDirty(table);
74+
EditorUtility.SetDirty(table.SharedData);
75+
76+
localizedString.TableReference = table.SharedData.TableCollectionNameGuid;
77+
localizedString.TableEntryReference = entry.KeyId;
78+
property.boxedValue = localizedString;
79+
}
80+
}
81+
82+
EditorGUI.EndProperty();
83+
}
84+
}
85+
86+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
87+
{
88+
if (property.isExpanded)
89+
{
90+
return EditorGUI.GetPropertyHeight(property, true) + EditorGUIUtility.singleLineHeight;
91+
}
92+
93+
var attr = attribute as SimpleLocalizedStringAttribute;
94+
return attr.Multiline ? EditorGUIUtility.singleLineHeight * 3 : EditorGUIUtility.singleLineHeight;
95+
}
96+
}
97+
}

Packages/com.starasgames.unity-clean-localized-string-inspector/Editor/SimpleLocalizedStringDrawer.cs.meta

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

Packages/com.starasgames.unity-clean-localized-string-inspector/Runtime/CleanLocalizedStringInspector.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "CleanLocalizedStringInspector",
33
"rootNamespace": "CleanLocalizedStringInspector",
4-
"references": [],
4+
"references": [
5+
"Unity.Localization"
6+
],
57
"includePlatforms": [],
68
"excludePlatforms": [],
79
"allowUnsafeCode": false,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using UnityEngine;
2+
3+
namespace Utils.SimpleLocalizedStringDrawer
4+
{
5+
public class SimpleLocalizedStringAttribute : PropertyAttribute
6+
{
7+
public string TableName { get; }
8+
public bool Multiline { get; }
9+
10+
public SimpleLocalizedStringAttribute(string tableName, bool multiline = false)
11+
{
12+
TableName = tableName;
13+
Multiline = multiline;
14+
}
15+
}
16+
}

Packages/com.starasgames.unity-clean-localized-string-inspector/Runtime/SimpleLocalizedStringAttribute.cs.meta

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

Packages/com.starasgames.unity-clean-localized-string-inspector/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"path": "Samples~/SampleFolderName"
2121
}
2222
],
23-
"hideInEditor": true
23+
"hideInEditor": true,
24+
"dependencies": {
25+
"com.unity.localization": "1.5.9"
26+
}
2427
}

Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"com.unity.ide.rider": "3.0.38",
34
"com.unity.modules.assetbundle": "1.0.0",
45
"com.unity.modules.audio": "1.0.0",
56
"com.unity.modules.director": "1.0.0",

Packages/packages-lock.json

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,78 @@
44
"version": "file:com.starasgames.unity-clean-localized-string-inspector",
55
"depth": 0,
66
"source": "embedded",
7-
"dependencies": {}
7+
"dependencies": {
8+
"com.unity.localization": "1.5.9"
9+
}
10+
},
11+
"com.unity.addressables": {
12+
"version": "1.25.0",
13+
"depth": 2,
14+
"source": "registry",
15+
"dependencies": {
16+
"com.unity.test-framework": "1.1.33",
17+
"com.unity.modules.assetbundle": "1.0.0",
18+
"com.unity.modules.jsonserialize": "1.0.0",
19+
"com.unity.modules.imageconversion": "1.0.0",
20+
"com.unity.modules.unitywebrequest": "1.0.0",
21+
"com.unity.scriptablebuildpipeline": "1.22.4",
22+
"com.unity.modules.unitywebrequestassetbundle": "1.0.0"
23+
},
24+
"url": "https://packages.unity.com"
25+
},
26+
"com.unity.ext.nunit": {
27+
"version": "1.0.6",
28+
"depth": 1,
29+
"source": "registry",
30+
"dependencies": {},
31+
"url": "https://packages.unity.com"
32+
},
33+
"com.unity.ide.rider": {
34+
"version": "3.0.38",
35+
"depth": 0,
36+
"source": "registry",
37+
"dependencies": {
38+
"com.unity.ext.nunit": "1.0.6"
39+
},
40+
"url": "https://packages.unity.com"
41+
},
42+
"com.unity.localization": {
43+
"version": "1.5.9",
44+
"depth": 1,
45+
"source": "registry",
46+
"dependencies": {
47+
"com.unity.addressables": "1.25.0",
48+
"com.unity.nuget.newtonsoft-json": "3.0.2"
49+
},
50+
"url": "https://packages.unity.com"
51+
},
52+
"com.unity.nuget.newtonsoft-json": {
53+
"version": "3.2.1",
54+
"depth": 2,
55+
"source": "registry",
56+
"dependencies": {},
57+
"url": "https://packages.unity.com"
58+
},
59+
"com.unity.scriptablebuildpipeline": {
60+
"version": "1.22.4",
61+
"depth": 3,
62+
"source": "registry",
63+
"dependencies": {
64+
"com.unity.test-framework": "1.1.33",
65+
"com.unity.modules.assetbundle": "1.0.0"
66+
},
67+
"url": "https://packages.unity.com"
68+
},
69+
"com.unity.test-framework": {
70+
"version": "1.1.33",
71+
"depth": 3,
72+
"source": "registry",
73+
"dependencies": {
74+
"com.unity.ext.nunit": "1.0.6",
75+
"com.unity.modules.imgui": "1.0.0",
76+
"com.unity.modules.jsonserialize": "1.0.0"
77+
},
78+
"url": "https://packages.unity.com"
879
},
980
"com.unity.modules.animation": {
1081
"version": "1.0.0",

0 commit comments

Comments
 (0)