lvjincheng 4 лет назад
Родитель
Сommit
98d2a6802d

+ 0 - 8
Assets/BowArrow/Scripts/Components/TextAutoLanguage.cs

@@ -107,14 +107,6 @@ public class TextAutoLanguage : MonoBehaviour
         SetText(textID);
     }
 }
-[Serializable]
-class LanguageFontSize {
-    public LanguageEnum language;
-    public int fontSize;
-}
-public enum LanguageEnum {
-    Chinese, English
-}
 class LanguageDefault {
     public string text0 = "";
     public string text1 = "昵 称 :";

+ 8 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: ab96debb6d23c224db1399841dd52d27
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: acef37103ad77bc48895551e530cce2c
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: d7d0ed1b721b26f40976532d87e9f155
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 13 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/cn.json

@@ -0,0 +1,13 @@
+{
+    "course_title_1": "弓的介绍及护具佩戴",
+    "course_title_2": "热身",
+    "course_title_3": "站姿",
+    "course_title_4": "沉肩转臂",
+    "course_title_5": "搭箭推弓",
+    "course_title_6": "预开弓",
+    "course_title_7": "举弓开弓",
+    "course_title_8": "靠位",
+    "course_title_9": "瞄准",
+    "course_title_10": "撒放",
+    "course_title_11": "结束动作"
+}

+ 7 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/cn.json.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 6f4c21a4fc18ed540ae3702961b00c1e
+TextScriptImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 13 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/en.json

@@ -0,0 +1,13 @@
+{
+    "course_title_1": "Introduction of bow and equipment",
+    "course_title_2": "Warm up",
+    "course_title_3": "Standing posture",
+    "course_title_4": "Heavy shoulder swivel arm",
+    "course_title_5": "Take an arrow and push a bow",
+    "course_title_6": "Pre opening bow",
+    "course_title_7": "Raise the bow and open the bow",
+    "course_title_8": "Leaning position",
+    "course_title_9": "Aim",
+    "course_title_10": "Release",
+    "course_title_11": "Finish up job"
+}

+ 7 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/en.json.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 32dfb61e235e9184b8bf2b197bbbacff
+TextScriptImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 118 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/TextAutoLanguage2.cs

@@ -0,0 +1,118 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using Newtonsoft.Json;
+
+public class TextAutoLanguage2 : MonoBehaviour
+{
+    [SerializeField] string textKey;
+    [SerializeField] RectTransform layoutRebuildObject;
+    static LanguageEnum currentLanguageEnum = LanguageEnum.Chinese;
+    static Dictionary<string, string> languageDictionary = null;
+    static HashSet<TextAutoLanguage2> textAutoLanguages = new HashSet<TextAutoLanguage2>();
+    public string[] textFormatArgs = {};
+    [SerializeField] LanguageFontSize[] languageFontSizes = {};
+
+    private static bool inited = false;
+    public static void Init()
+    {
+        if (inited) return;
+        inited = true;
+        int id = PlayerPrefs.GetInt("Language", 0);
+        ChangeLanguage((LanguageEnum) id);
+    }
+
+    public static void ChangeLanguage(LanguageEnum languageEnum) 
+    {
+        string fileName = "cn";
+        if (languageEnum == LanguageEnum.English) fileName = "en";
+        languageDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(Resources.Load<TextAsset>("TextAutoLanguage2/" + fileName).text);
+        currentLanguageEnum = languageEnum;
+        PlayerPrefs.SetInt("Language", ((int)languageEnum));
+        foreach (var textAutoLanguage in textAutoLanguages)
+        {
+            try {
+                textAutoLanguage.ApplyText();    
+            } catch (Exception e) { Debug.LogError(e.Message); }
+        }
+    }
+
+    public static LanguageEnum GetLanguage()
+    {
+        return currentLanguageEnum;
+    }
+
+    public static string GetTextByKey(string textKey) {
+        Init();
+        return languageDictionary[textKey];
+    }
+
+    void Awake()
+    {
+        Init();
+    }
+
+    void Start()
+    {
+        textAutoLanguages.Add(this);
+        ApplyText();   
+    }
+
+    void OnDestroy()
+    {
+        textAutoLanguages.Remove(this);
+    }
+
+    /**记录组件的默认字体大小 */
+    private int defaultFontSize = -1;
+    public void SetTextKey(string textKey)
+    {
+        this.textKey = textKey;
+        string text = languageDictionary[textKey];
+        if (textFormatArgs.Length > 0)
+        {
+            text = String.Format(text, textFormatArgs);
+        }
+        Text textComp = GetComponent<Text>();
+        textComp.text = text;
+        //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
+        if (defaultFontSize == -1) { //首次记录组件的默认字体大小
+            defaultFontSize = textComp.fontSize;
+        } else {
+            textComp.fontSize = defaultFontSize;
+        }
+        if (languageFontSizes.Length > 0) {
+            foreach (var languageFontSize in languageFontSizes)
+            {
+                if (languageFontSize.language == currentLanguageEnum) {
+                    textComp.fontSize = languageFontSize.fontSize;
+                    break;
+                }
+            }
+        }
+        //重置指定节点的布局
+        if (layoutRebuildObject)
+        {
+            LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject);
+        }
+    }
+
+    public string GetTextKey()
+    {
+        return this.textKey;
+    }
+
+    void ApplyText()
+    {
+        SetTextKey(textKey);
+    }
+}
+[Serializable]
+public class LanguageFontSize {
+    public LanguageEnum language;
+    public int fontSize;
+}
+public enum LanguageEnum {
+    Chinese, English
+}

+ 11 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/TextAutoLanguage2.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 0f4efe98aab6c6b41a7ee1f4c49df27b
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 4 - 19
Assets/BowArrow/Scripts/View/CourseView.cs

@@ -7,19 +7,6 @@ public class CourseView : MonoBehaviour
 {
     [SerializeField] GameObject scrollViewContent;
     [SerializeField] GameObject scrollViewContentItem;
-    string[] titles = {
-        "弓的介绍及护具佩戴",
-        "热身",
-        "站姿",
-        "沉肩转臂",
-        "搭箭推弓",
-        "预开弓",
-        "举弓开弓",
-        "靠位",
-        "瞄准",
-        "撒放",
-        "结束动作"
-    };
 
     string[] videoUrls = {
         "https://www.bilibili.com/video/BV1WW41167iT?spm_id_from=333.999.0.0",
@@ -37,18 +24,16 @@ public class CourseView : MonoBehaviour
 
     public void Awake() {
         scrollViewContentItem.SetActive(false);
-        int id = 0;
-        foreach (string title in titles)
+        for (int i = 1; i <= videoUrls.Length; i++)
         {
-            id++;
-            int currentID = id;
+            string title = TextAutoLanguage2.GetTextByKey("course_title_" + i);
             GameObject item = Instantiate<GameObject>(scrollViewContentItem, scrollViewContent.transform);
-            item.GetComponent<Text>().text = currentID + "、" + title;
+            item.GetComponent<Text>().text = i + "、" + title;
             item.GetComponentInChildren<Button>().onClick.AddListener(delegate() {
                 // GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/VideoView"));
                 // VideoView videoView = o.GetComponent<VideoView>();
                 // videoView.url = $"https://smartbow-course-1258277015.cos.ap-guangzhou.myqcloud.com/{currentID}.mp4";
-                Application.OpenURL(videoUrls[currentID - 1]);
+                Application.OpenURL(videoUrls[i - 1]);
             });
             item.SetActive(true);
         }

+ 2 - 0
Assets/BowArrow/Scripts/View/SetUpView.cs

@@ -114,9 +114,11 @@ public class SetUpView : MonoBehaviour
         if (TextAutoLanguage.GetLanguage() == LanguageEnum.English)
         {
             TextAutoLanguage.ChangeLanguage(LanguageEnum.Chinese);
+            TextAutoLanguage2.ChangeLanguage(LanguageEnum.Chinese);
         }
         else if (TextAutoLanguage.GetLanguage() == LanguageEnum.Chinese) {
             TextAutoLanguage.ChangeLanguage(LanguageEnum.English);
+            TextAutoLanguage2.ChangeLanguage(LanguageEnum.English);
         }
     }