183 lines
5.3 KiB
C#
183 lines
5.3 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class StarRating : MonoBehaviour
|
||
{
|
||
[Header("星星設定")]
|
||
public Button[] stars;
|
||
public Sprite starGray;
|
||
public Sprite starYellow;
|
||
|
||
private int currentRating = 0;
|
||
|
||
void Awake()
|
||
{
|
||
Debug.Log("=== " + gameObject.name + " 開始初始化 ===");
|
||
|
||
// 檢查 EventSystem
|
||
EventSystem eventSystem = FindObjectOfType<EventSystem>();
|
||
if (eventSystem == null)
|
||
{
|
||
Debug.LogError("場景中沒有 EventSystem!");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("EventSystem 存在: " + eventSystem.name);
|
||
}
|
||
|
||
// 檢查圖片
|
||
if (starGray == null)
|
||
{
|
||
Debug.LogError(gameObject.name + ": 灰色星星圖片沒有指派!");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("灰色星星圖片已指派");
|
||
}
|
||
|
||
if (starYellow == null)
|
||
{
|
||
Debug.LogError(gameObject.name + ": 黃色星星圖片沒有指派!");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("黃色星星圖片已指派");
|
||
}
|
||
|
||
Debug.Log("星星陣列大小: " + stars.Length);
|
||
|
||
// 初始化每顆星星
|
||
for (int i = 0; i < stars.Length; i++)
|
||
{
|
||
if (stars[i] == null)
|
||
{
|
||
Debug.LogError("星星 " + i + " 沒有被指派!");
|
||
continue;
|
||
}
|
||
|
||
int starIndex = i + 1;
|
||
|
||
// 設定初始圖片
|
||
Image starImage = stars[i].GetComponent<Image>();
|
||
if (starImage != null && starGray != null)
|
||
{
|
||
starImage.sprite = starGray;
|
||
starImage.raycastTarget = true;
|
||
Debug.Log("星星 " + starIndex + " 圖片設定完成,Raycast Target = " + starImage.raycastTarget);
|
||
}
|
||
|
||
// 檢查 Button
|
||
Button button = stars[i].GetComponent<Button>();
|
||
if (button != null)
|
||
{
|
||
button.interactable = true;
|
||
|
||
Debug.Log("星星 " + starIndex + " Button 檢查:");
|
||
Debug.Log(" - Interactable: " + button.interactable);
|
||
Debug.Log(" - GameObject active: " + stars[i].gameObject.activeInHierarchy);
|
||
|
||
// 清除舊的監聽器
|
||
button.onClick.RemoveAllListeners();
|
||
|
||
// 添加新的監聽器
|
||
int clickedIndex = starIndex;
|
||
// 使用 Lambda 表達式確保正確傳遞索引值
|
||
button.onClick.AddListener(() => { OnStarClicked(clickedIndex); });
|
||
|
||
Debug.Log("星星 " + starIndex + " 點擊事件已綁定");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("星星 " + starIndex + " 沒有 Button 組件!");
|
||
}
|
||
}
|
||
|
||
Debug.Log("=== " + gameObject.name + " 初始化結束 ===");
|
||
}
|
||
|
||
// *** 移除的 Update 函數原本在這裡,它導致了 InvalidOperationException 錯誤 ***
|
||
// 因為按鈕的點擊事件已經在 Start() 中綁定,故此 Update 函數不需要。
|
||
|
||
void OnStarClicked(int starIndex)
|
||
{
|
||
Debug.Log("========================================");
|
||
Debug.Log("!!! " + gameObject.name + " 的星星 " + starIndex + " 被點擊 !!!");
|
||
Debug.Log("========================================");
|
||
SetRating(starIndex);
|
||
}
|
||
|
||
public void SetRating(int rating)
|
||
{
|
||
Debug.Log(">>> 設定評分: " + rating + " 顆星");
|
||
currentRating = rating;
|
||
UpdateStarDisplay();
|
||
}
|
||
|
||
void UpdateStarDisplay()
|
||
{
|
||
Debug.Log(">>> 開始更新星星顯示,目標評分: " + currentRating);
|
||
|
||
for (int i = 0; i < stars.Length; i++)
|
||
{
|
||
if (stars[i] == null) continue;
|
||
|
||
Image starImage = stars[i].GetComponent<Image>();
|
||
if (starImage != null)
|
||
{
|
||
if (i < currentRating)
|
||
{
|
||
if (starYellow != null)
|
||
{
|
||
starImage.sprite = starYellow;
|
||
Debug.Log(">>> 星星 " + (i+1) + " 更換為黃色圖片");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("黃色星星圖片是 null!");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (starGray != null)
|
||
{
|
||
starImage.sprite = starGray;
|
||
Debug.Log(">>> 星星 " + (i+1) + " 保持灰色圖片");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("灰色星星圖片是 null!");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
Debug.Log(">>> 星星顯示更新完成!");
|
||
}
|
||
|
||
public int GetRating()
|
||
{
|
||
return currentRating;
|
||
}
|
||
|
||
public void ResetRating()
|
||
{
|
||
currentRating = 0;
|
||
UpdateStarDisplay();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 設定為不能點擊
|
||
/// </summary>
|
||
public void SetNotInteractable(Color? color = null)
|
||
{
|
||
foreach (var button in stars)
|
||
{
|
||
button.interactable = false;
|
||
|
||
ColorBlock colors = button.colors;
|
||
colors.disabledColor = color ?? Color.white ;
|
||
button.colors = colors;
|
||
}
|
||
}
|
||
} |