197 lines
7.4 KiB
C#
197 lines
7.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.EventSystems;
|
||
using System;
|
||
using System.IO;
|
||
|
||
public class game_card_Prefab : MonoBehaviour, IPointerClickHandler
|
||
{
|
||
[Header("Prefab������Ϣ")]
|
||
public Image card_icon_image;
|
||
public Text card_name_text;
|
||
public Text authorName_text;
|
||
public Text versionCode_text;
|
||
public Text description_text;
|
||
public Text _isOfficial_text;
|
||
public Text source_text;
|
||
public Text yyyy_mm_dd;
|
||
|
||
[Header("Optional clickable Button (auto-find if null)")]
|
||
public Button clickButton;
|
||
|
||
// �洢��Ӧ index.html �ı���·�������� file:// ǰ��
|
||
private string indexFilePath;
|
||
|
||
// �� loadLittleGamesPrefab ����������ı���·��
|
||
public void SetData(string indexPath, string cardName, string authorName, string description, string isOfficial, string source, string yyyyMmDd, string version = null)
|
||
{
|
||
indexFilePath = indexPath;
|
||
if (card_name_text != null) card_name_text.text = cardName ?? "";
|
||
if (authorName_text != null) authorName_text.text = authorName ?? "";
|
||
if (description_text != null) description_text.text = description ?? "";
|
||
if (_isOfficial_text != null) _isOfficial_text.text = isOfficial ?? "";
|
||
if (source_text != null) source_text.text = source ?? "";
|
||
if (yyyy_mm_dd != null) yyyy_mm_dd.text = yyyyMmDd ?? "";
|
||
|
||
// version
|
||
string ver = string.IsNullOrEmpty(version) ? "0.0.0" : version;
|
||
if (versionCode_text != null) versionCode_text.text = ver;
|
||
|
||
UnityEngine.Debug.Log($"game_card_Prefab.SetData: indexPath='{indexFilePath}', card_name='{cardName}', author='{authorName}', isOfficial='{isOfficial}', source='{source}', version='{ver}'");
|
||
}
|
||
|
||
void Awake()
|
||
{
|
||
// If no explicit Button assigned, try to find one on this GameObject or children
|
||
if (clickButton == null)
|
||
{
|
||
clickButton = GetComponent<Button>();
|
||
if (clickButton == null)
|
||
clickButton = GetComponentInChildren<Button>(true);
|
||
}
|
||
|
||
if (clickButton == null)
|
||
{
|
||
// Try to auto-add a Button to this GameObject so clicks are captured.
|
||
// Ensure there's an Image component (Button requires Image for Raycast target)
|
||
Image img = GetComponent<Image>();
|
||
if (img == null)
|
||
{
|
||
img = gameObject.AddComponent<Image>();
|
||
img.color = new Color(1, 1, 1, 0); // transparent
|
||
}
|
||
|
||
clickButton = gameObject.AddComponent<Button>();
|
||
UnityEngine.Debug.Log("game_card_Prefab.Awake: No Button found; added Image+Button to prefab at runtime to capture clicks.");
|
||
}
|
||
|
||
// Check EventSystem presence
|
||
if (EventSystem.current == null)
|
||
{
|
||
UnityEngine.Debug.LogWarning("game_card_Prefab.Awake: No EventSystem in scene. UI clicks and IPointerClickHandler won't work without an EventSystem.");
|
||
}
|
||
}
|
||
|
||
void OnEnable()
|
||
{
|
||
if (clickButton != null)
|
||
{
|
||
clickButton.onClick.AddListener(OnButtonClicked);
|
||
UnityEngine.Debug.Log("game_card_Prefab: added onClick listener to Button");
|
||
}
|
||
}
|
||
|
||
void OnDisable()
|
||
{
|
||
if (clickButton != null)
|
||
{
|
||
clickButton.onClick.RemoveListener(OnButtonClicked);
|
||
UnityEngine.Debug.Log("game_card_Prefab: removed onClick listener from Button");
|
||
}
|
||
}
|
||
|
||
void OnButtonClicked()
|
||
{
|
||
UnityEngine.Debug.Log($"game_card_Prefab.OnButtonClicked: indexPath='{indexFilePath}'");
|
||
OpenIndexPath();
|
||
}
|
||
|
||
// ���ʱ����ͨ�� WebViewLauncher ��Ӧ�� index.html
|
||
public void OnPointerClick(PointerEventData eventData)
|
||
{
|
||
UnityEngine.Debug.Log($"game_card_Prefab.OnPointerClick: indexPath='{indexFilePath}'");
|
||
OpenIndexPath();
|
||
}
|
||
|
||
void OpenIndexPath()
|
||
{
|
||
if (string.IsNullOrEmpty(indexFilePath))
|
||
{
|
||
UnityEngine.Debug.LogWarning("game_card_Prefab clicked but index path is empty");
|
||
return;
|
||
}
|
||
|
||
// ����ʹ�� WebViewLauncher ����
|
||
if (WebViewLauncher.Instance != null)
|
||
{
|
||
UnityEngine.Debug.Log("Calling WebViewLauncher.OpenUrl with: " + indexFilePath);
|
||
WebViewLauncher.Instance.OpenUrl(indexFilePath);
|
||
|
||
// After initiating navigation, set the input field display to localhost::parent/index.html
|
||
try
|
||
{
|
||
if (WebViewLauncher.Instance != null && WebViewLauncher.Instance.urlInputField != null)
|
||
{
|
||
string parent = Path.GetFileName(Path.GetDirectoryName(indexFilePath));
|
||
string display = $"localhost::{parent}/{Path.GetFileName(indexFilePath)}";
|
||
WebViewLauncher.Instance.urlInputField.text = display;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
UnityEngine.Debug.LogWarning("Failed to set url input display: " + e.Message);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
UnityEngine.Debug.LogWarning("WebViewLauncher instance not found. Attempting to start new launcher.");
|
||
// ���Բ��ҳ����е� WebViewLauncher ��һ���µ� GameObject
|
||
var launcherObj = UnityEngine.Object.FindAnyObjectByType<WebViewLauncher>();
|
||
if (launcherObj != null)
|
||
{
|
||
UnityEngine.Debug.Log("Found launcher in scene, calling OpenUrl: " + indexFilePath);
|
||
launcherObj.OpenUrl(indexFilePath);
|
||
|
||
try
|
||
{
|
||
if (launcherObj.urlInputField != null)
|
||
{
|
||
string parent = Path.GetFileName(Path.GetDirectoryName(indexFilePath));
|
||
string display = $"localhost::{parent}/{Path.GetFileName(indexFilePath)}";
|
||
launcherObj.urlInputField.text = display;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
UnityEngine.Debug.LogWarning("Failed to set url input display: " + e.Message);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// Create a temporary launcher object to start webview
|
||
UnityEngine.Debug.Log("Creating temporary WebViewLauncher and calling OpenUrl: " + indexFilePath);
|
||
GameObject go = new GameObject("WebViewLauncher_Temp");
|
||
var launcher = go.AddComponent<WebViewLauncher>();
|
||
launcher.OpenUrl(indexFilePath);
|
||
GameObject.DontDestroyOnLoad(go);
|
||
|
||
try
|
||
{
|
||
if (launcher.urlInputField != null)
|
||
{
|
||
string parent = Path.GetFileName(Path.GetDirectoryName(indexFilePath));
|
||
string display = $"localhost::{parent}/{Path.GetFileName(indexFilePath)}";
|
||
launcher.urlInputField.text = display;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
UnityEngine.Debug.LogWarning("Failed to set url input display: " + e.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||
void Start()
|
||
{
|
||
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
}
|