using UnityEngine; using DG.Tweening; using UnityEngine.UI; public class Bannnner : MonoBehaviour { [Header("UI References")] [SerializeField] private RectTransform[] imageItems; [SerializeField] private Button leftButton; [SerializeField] private Button rightButton; [SerializeField] private Button[] jumpButtons; [Header("Settings")] [SerializeField] private float animationDuration = 0.5f; [SerializeField] private Color selectedButtonColor = Color.white; [SerializeField] private Color normalButtonColor = new Color(0.8f, 0.8f, 0.8f, 1f); private bool isAnimating; private int[] itemIndices = new int[5]; private const int CENTER_INDEX = 2; private readonly Vector2[] positions = new Vector2[5] { new Vector2(-800, 0), new Vector2(-400, 0), new Vector2(0, 0), new Vector2(400, 0), new Vector2(800, 0) }; private readonly Vector3[] scales = new Vector3[5] { new Vector3(0.6f, 0.6f, 1), new Vector3(0.8f, 0.8f, 1), new Vector3(1f, 1f, 1), new Vector3(0.8f, 0.8f, 1), new Vector3(0.6f, 0.6f, 1) }; private void Start() { InitializeCarousel(); SetupButtons(); } private void InitializeCarousel() { for (int i = 0; i < imageItems.Length; i++) { itemIndices[i] = i; imageItems[i].anchoredPosition = positions[i]; imageItems[i].localScale = scales[i]; } UpdateButtonsState(); } private void SetupButtons() { leftButton.onClick.AddListener(() => MoveCarousel(1)); rightButton.onClick.AddListener(() => MoveCarousel(-1)); for (int i = 0; i < jumpButtons.Length; i++) { int index = i; jumpButtons[i].onClick.AddListener(() => JumpToImage(index)); } } private void MoveCarousel(int step) { if (isAnimating) return; isAnimating = true; // 更新索引 int[] newIndices = new int[5]; for (int i = 0; i < 5; i++) { newIndices[i] = itemIndices[(i + step + 5) % 5]; } itemIndices = newIndices; AnimateItems(() => { isAnimating = false; UpdateButtonsState(); }); } private void JumpToImage(int targetImageIndex) { if (isAnimating) return; isAnimating = true; // 计算需要移动的步数 int currentPosIndex = System.Array.IndexOf(itemIndices, targetImageIndex); int stepsToMove = CENTER_INDEX - currentPosIndex; // 更新索引 int[] newIndices = new int[5]; for (int i = 0; i < 5; i++) { int newPos = (i + stepsToMove + 5) % 5; newIndices[newPos] = itemIndices[i]; } itemIndices = newIndices; AnimateItems(() => { isAnimating = false; UpdateButtonsState(); }); } private void AnimateItems(TweenCallback onComplete) { Sequence sequence = DOTween.Sequence(); for (int i = 0; i < imageItems.Length; i++) { int targetPosIndex = itemIndices[i]; sequence.Join(imageItems[i].DOAnchorPos(positions[targetPosIndex], animationDuration)); sequence.Join(imageItems[i].DOScale(scales[targetPosIndex], animationDuration)); } sequence.OnComplete(onComplete); } private void UpdateButtonsState() { int centerImageIndex = itemIndices[CENTER_INDEX]; for (int i = 0; i < jumpButtons.Length; i++) { bool isSelected = (i == centerImageIndex); jumpButtons[i].interactable = !isSelected; if (jumpButtons[i].TryGetComponent(out Image buttonImage)) { buttonImage.color = isSelected ? selectedButtonColor : normalButtonColor; } } } private void OnDestroy() { DOTween.Kill(gameObject); } } ----------------------------- if (run) { animator.SetBool("isRun", true); this.transform.Translate(0, 0, speed * Time.deltaTime); if(Mathf.Abs(this.transform.position.z - currentPositionZ) >= 10) { run = false; animator.SetBool("isRun", false); this.currentPositionZ = this.transform.position.z; } } --------------------------------- using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CCC : MonoBehaviour { private CharacterController characterController; public float speed = 12f; public float g = 7f; private Vector3 moveDirction; public Transform cam; private float rotx; private float roty; public float mouseSpeed; public GameObject ljt; bool isSelected = false; public GameObject images; public Text text; // Start is called before the first frame update void Start() { characterController = GetComponent(); } // Update is called once per frame void Update() { Move(); Cam2(); Ray(); PrintText(); } public void Closetext() { images.SetActive(false); text.text = ""; } private void PrintText() { if (isSelected && Input.GetKeyDown(KeyCode.Mouse0)) { images.SetActive(true); if (images.active) { String str = "谢婷,牛逼,六角亭,牛逼牛逼,牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼牛逼"; text.DOText(str, 1.5f).SetEase(Ease.Linear).OnComplete(() => { Debug.Log("jiesu"); if (!images.active) { text.text = ""; } }); } } } private void Cam2() { rotx += cam.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSpeed; roty -= Input.GetAxis("Mouse Y")*mouseSpeed; roty = Mathf.Clamp(roty, -45f, 45f); this.transform.eulerAngles = new Vector3(0, rotx, 0); cam.transform.eulerAngles = new Vector3(roty,rotx, 0); } private void Move() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (characterController.isGrounded) { moveDirction = new Vector3(horizontal, 0, vertical); if (Input.GetKey(KeyCode.Space)) { moveDirction.y = 2; } } moveDirction.y -= g * Time.deltaTime; characterController.Move(characterController.transform.TransformDirection(moveDirction * speed * Time.deltaTime)); } void Ray() { Vector3 start = cam.transform.position; Vector3 dir = cam.transform.forward; float dis = 10; RaycastHit hit; if(Physics.Raycast(start,dir,out hit, dis)) { if(hit.transform.name == "shitou") { ljt.transform.GetComponent().enabled = true; isSelected = true; } else { ljt.transform.GetComponent().enabled = false; isSelected = false; } } } }