虽然之前的Button相关的脚本写的确实是屎山,但是我在Earthhitting场景里仍然复用了这些脚本
因为当时为了确保实现脚本的解耦合,我做了一些放到Framework命名空间标明这是可以复用的脚本
这里所有脚本和上一篇中使用的一模一样

唯一的区别就是给按钮增加了两个shader效果,这个我之前提到过就不说了,反正我是会用shader但是不会写shader
这里多写了一个脚本放到背景图上让它循环向左移动
C#已复制
using UnityEngine;
namespace MyAssets.Framework
{
public class FloatingItem : MonoBehaviour
{
[]
public float floatAmplitude = 0.5f; // 上下浮动的幅度
public float floatSpeed = 1f; // 浮动速度
private Vector3 startPos;
void Start()
{
startPos = transform.position;
}
void Update()
{
float newY = startPos.y + Mathf.Sin(Time.time * floatSpeed) * floatAmplitude;
transform.position = new Vector3(startPos.x, newY, startPos.z);
}
}
}
物体公转
C#已复制
using UnityEngine;
namespace MyAssets.Framework
{
[]
public class ItemRevolution : MonoBehaviour
{
[]
public Transform center;
[]
public float orbitRadius = 2f;
[]
public float orbitSpeed = 90f;
[]
public bool counterClockwise = true;
[]
public Color orbitColor = Color.cyan;
[]
public int orbitSegments = 64;
// 记录当前角度
private float currentAngle;
void Start()
{
//判断中心点是否为空,若为空则设为父物体
if (center == null)
{
center = transform.parent;
}else if(center != null)
{
Vector2 offset = transform.position - center.position;
currentAngle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
orbitRadius = offset.magnitude;
}
}
void Update()
{
//只有在运行游戏时才移动
if (!Application.isPlaying)
{
return;
}
if (!center)
{
return;
}
float direction = counterClockwise ? 1f : -1f;
currentAngle += orbitSpeed * direction * Time.deltaTime;
float rad = currentAngle * Mathf.Deg2Rad;
Vector2 offset = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)) * orbitRadius;
transform.position = (Vector2)center.position + offset;
}
void OnDrawGizmos()
{
if (center == null) return;
Gizmos.color = orbitColor;
Vector3 prevPos = center.position + new Vector3(orbitRadius, 0, 0);
for (int i = 1; i <= orbitSegments; i++)
{
float angle = i * (360f / orbitSegments) * Mathf.Deg2Rad;
Vector3 nextPos = center.position + new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * orbitRadius;
Gizmos.DrawLine(prevPos, nextPos);
prevPos = nextPos;
}
}
}
}
物体自转
C#已复制
using UnityEngine;
namespace MyAssets.Framework
{
public class ItemRotation : MonoBehaviour
{
[]
[]
public float rotationSpeed = 90f;
[]
public bool counterClockwise = true;
void Update()
{
float direction = counterClockwise ? 1f : -1f;
transform.Rotate(0, 0, rotationSpeed * direction * Time.deltaTime);
}
}
}


评论区