Skip to content

Commit

Permalink
选项菜单按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
TianMengLucky committed Aug 24, 2023
1 parent 2e98c5f commit 18b5d1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
38 changes: 29 additions & 9 deletions NextShip/Options/Patches/OptionsConsolePatch.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using HarmonyLib;
using NextShip.Utilities;
using UnityEngine;
using AmongUs.QuickChat;
using TMPro;
using Object = UnityEngine.Object;

namespace NextShip.Options.Patches;

Expand Down Expand Up @@ -67,27 +69,42 @@ public static bool InitNextOptionMenu(OptionsConsole __instance)
NextMenu = new GameObject("NextMenu");
NextMenu.transform.SetParent(NextMenuParent.transform);
NextMenuParent.SetActive(false);
NextMenuParent.AllGameObjectDo(n => n.layer = tint.gameObject.layer);
return true;

GameObject CreateButton(string Title, string text, string name)
GameObject CreateButton(string Title, string text, string name, Action action = null)
{
GameObject button = new GameObject(name);
button.layer = tint.gameObject.layer;
button.transform.localPosition = new(-3.6f, 0, 0);
button.transform.SetParent(NextMenuParent.transform);
button.CreatePassiveButton();
button.CreatePassiveButton(onClick:action);

var backGround = new GameObject("BackGround");
backGround.transform.SetParent(button.transform);
backGround.transform.localPosition = new(0, 0, 0);

var backGroundSprite = backGround.AddComponent<SpriteRenderer>();
backGroundSprite.sprite = ObjetUtils.Find<Sprite>("button");
backGroundSprite.sprite = ObjetUtils.Find<Sprite>("buttonClick");
backGroundSprite.drawMode = SpriteDrawMode.Sliced;
backGroundSprite.size = new(2.5f, 1.3f);

button.AddComponent<BoxCollider2D>().size = backGroundSprite.size;

var titleTextGameObject = new GameObject("TitleText");
titleTextGameObject.transform.SetParent(button.transform);
titleTextGameObject.AddComponent<TextMeshPro>().text = Title;
titleTextGameObject.transform.localPosition = new(9.4f, -2.2f, 0);

/*var SubTextGameObject = Object.Instantiate(TitleGameObject);
SubTextGameObject.name = "SubText";*/
var textMeshPro = titleTextGameObject.AddComponent<TextMeshPro>();
textMeshPro.text = Title;
textMeshPro.fontSize = 5;

var SubTextGameObject = Object.Instantiate(titleTextGameObject);
SubTextGameObject.name = "SubText";
SubTextGameObject.transform.localPosition = new(8, -1.3f, 0);

var SubTextMeshPro = SubTextGameObject.GetComponent<TextMeshPro>();
SubTextMeshPro.text = text;
SubTextMeshPro.fontSize = 3;

return button;
}
Expand All @@ -97,7 +114,10 @@ public static void OpenNextOptionMenu(OptionsConsole __instance)
{
if (!Camera.main) return;
PlayerControl.LocalPlayer.NetTransform.Halt();
var optionMenu = Object.Instantiate(NextMenuParent, Camera.main.transform, false);
GameObject optionMenu;
if (!(optionMenu = GameObject.Find("NextMenuParent(Clone)")))
optionMenu = Object.Instantiate(NextMenuParent, Camera.main.transform, false);

optionMenu.transform.localPosition = __instance.CustomPosition;
FastDestroyableSingleton<TransitionFade>.Instance.DoTransitionFade(null, optionMenu, null);
IsNextMenu = true;
Expand Down
11 changes: 11 additions & 0 deletions NextShip/Utils/GameObjectUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public static GameObject CreateGameObject(string name = null, Transform parent =
if (parent != null) gameObject.transform.SetParent(parent);
return gameObject;
}

public static void AllGameObjectDo(this GameObject gameObject, Action<GameObject> action)
{
action(gameObject);
for (int i = 0; i < gameObject.transform.childCount; i++)
{
GameObject obj = gameObject.transform.GetChild(i).gameObject;
action(obj);
obj.AllGameObjectDo(action);
}
}
}
9 changes: 7 additions & 2 deletions NextShip/Utils/ObjetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using NextShip.Utilities;
using UnityEngine;

namespace NextShip.Utils;
Expand All @@ -12,14 +13,18 @@ public static class ObjetUtils
public static List<Object> AllObjects = new ();
public static List<Object> AllCacheObject = new ();

public static T Find<T>(string name) where T : Il2CppObjectBase
public static T Find<T>(string name) where T : Il2CppObjectBase
{
var FindObj = AllCacheObject.FirstOrDefault(n => n.name == name);
if (FindObj) return FindObj.CastFast<T>();
foreach (var obj in Resources.FindObjectsOfTypeAll(Il2CppType.Of<T>()))
{
if (obj.name == name)
{
Info($"ObjectUtils.Find return isnull:{false} Find<{typeof(T).Name}> Get:{name}");
return obj.Cast<T>();
AllCacheObject.Add(obj);
Object.DontDestroyOnLoad(obj);
return obj.CastFast<T>();
}
}
Info($"ObjectUtils.Find return isnull:{true} Find<{typeof(T).Name}> Get:{name}");
Expand Down

0 comments on commit 18b5d1b

Please sign in to comment.