105 lines
3.9 KiB
C#
105 lines
3.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class Notepad : MonoBehaviour
|
|
{
|
|
public AppLayer appLayer;
|
|
private TextMeshProUGUI titleText;
|
|
private TMP_InputField inputField;
|
|
|
|
void Awake()
|
|
{
|
|
BuildUI();
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (appLayer != null)
|
|
{
|
|
appLayer.sizeDelta = new Vector2(600, 400);
|
|
appLayer.appMinSize = new Vector2(300, 200);
|
|
appLayer.color = new Color(31, 31, 31);
|
|
}
|
|
}
|
|
|
|
void BuildUI()
|
|
{
|
|
// Ãëàâíûé êîíòåéíåð
|
|
GameObject mainV = new GameObject("MainV", typeof(RectTransform), typeof(VerticalLayoutGroup));
|
|
mainV.transform.SetParent(transform, false);
|
|
Stretch(mainV.GetComponent<RectTransform>());
|
|
|
|
var vGroup = mainV.GetComponent<VerticalLayoutGroup>();
|
|
vGroup.childControlWidth = true; vGroup.childControlHeight = true;
|
|
vGroup.childForceExpandHeight = false;
|
|
|
|
// --- ÂÅÐÕ: Çàãîëîâîê (Âûñîòà 20) ---
|
|
GameObject header = new GameObject("Header", typeof(RectTransform), typeof(Image));
|
|
header.transform.SetParent(mainV.transform, false);
|
|
header.GetComponent<Image>().color = new Color(0.8f, 0.8f, 0.8f);
|
|
header.AddComponent<LayoutElement>().preferredHeight = 20;
|
|
|
|
titleText = CreateTMPText("Íîâûé òåêñòîâûé äîêóìåíò.txt", header.transform, 12, Color.black);
|
|
titleText.alignment = TextAlignmentOptions.Left;
|
|
titleText.rectTransform.offsetMin = new Vector2(5, 0);
|
|
|
|
// --- ÖÅÍÒÐ: Ïîëå ââîäà (Çàíèìàåò âñ¸ îñòàëüíîå) ---
|
|
GameObject scrollGo = new GameObject("TextScroll", typeof(RectTransform), typeof(ScrollRect), typeof(Image));
|
|
scrollGo.transform.SetParent(mainV.transform, false);
|
|
scrollGo.AddComponent<LayoutElement>().flexibleHeight = 1;
|
|
|
|
// Ïîëå ââîäà TMP
|
|
GameObject inputGo = new GameObject("InputField", typeof(RectTransform), typeof(TMP_InputField));
|
|
inputGo.transform.SetParent(scrollGo.transform, false);
|
|
Stretch(inputGo.GetComponent<RectTransform>());
|
|
|
|
inputField = inputGo.GetComponent<TMP_InputField>();
|
|
inputField.lineType = TMP_InputField.LineType.MultiLineNewline;
|
|
|
|
// Òåêñòîâàÿ îáëàñòü âíóòðè InputField
|
|
GameObject textArea = new GameObject("TextArea", typeof(RectTransform), typeof(RectMask2D));
|
|
textArea.transform.SetParent(inputGo.transform, false);
|
|
Stretch(textArea.GetComponent<RectTransform>());
|
|
textArea.GetComponent<RectTransform>().offsetMin = new Vector2(5, 5);
|
|
|
|
GameObject textDisplay = new GameObject("Text", typeof(RectTransform), typeof(TextMeshProUGUI));
|
|
textDisplay.transform.SetParent(textArea.transform, false);
|
|
Stretch(textDisplay.GetComponent<RectTransform>());
|
|
|
|
var t = textDisplay.GetComponent<TextMeshProUGUI>();
|
|
t.color = Color.black;
|
|
t.fontSize = 14;
|
|
|
|
inputField.textViewport = textArea.GetComponent<RectTransform>();
|
|
inputField.textComponent = t;
|
|
}
|
|
|
|
// Ìåòîä äëÿ îòêðûòèÿ ôàéëà
|
|
public void OpenFile(string fileName, string content)
|
|
{
|
|
if (titleText != null) titleText.text = fileName;
|
|
if (inputField != null) inputField.text = content;
|
|
|
|
// Äåëàåì îêíî àêòèâíûì ÷åðåç AppLayer
|
|
if (appLayer != null) appLayer.Show();
|
|
}
|
|
|
|
// Âñïîìîãàòåëüíûå ìåòîäû
|
|
TextMeshProUGUI CreateTMPText(string content, Transform parent, int size, Color col)
|
|
{
|
|
GameObject go = new GameObject("Text", typeof(RectTransform), typeof(TextMeshProUGUI));
|
|
go.transform.SetParent(parent, false);
|
|
Stretch(go.GetComponent<RectTransform>());
|
|
var t = go.GetComponent<TextMeshProUGUI>();
|
|
t.text = content; t.fontSize = size; t.color = col;
|
|
t.font = TMP_Settings.defaultFontAsset;
|
|
return t;
|
|
}
|
|
|
|
void Stretch(RectTransform rt)
|
|
{
|
|
rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one;
|
|
rt.offsetMin = Vector2.zero; rt.offsetMax = Vector2.zero;
|
|
}
|
|
} |