Unity背景图⽆限循环
背景⽆限循环
⾸先我们导⼊⼀张没有被处理过的图⽚
我们使⽤图⽚原来的⼤⼩。
让后给这个精灵(我喜欢叫他雪碧,因为这是⼀个不错的饮料)添加我们的脚本。
组件代码:
/**********
* author        :  "⼩⽩⾍||镜⼦"
* time          :  "2020-8-27"
* describe      :  "背景循环流动"
*  unity version :  "2020.3.16"
*  Blog          :  "wwwblogs/jzyl"
**********/
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public enum Direction
{
[Tooltip("向上流动")]
UP = 0,
[Tooltip("向下流动")]
DOWN,
[Tooltip("向左流动")]
LEFT,
[Tooltip("向右流动")]
RIGHT,
}
public class BackgroundLoop : MonoBehaviour
{
[ReadOnly, SerializeField]
private string 注意 = "图⽚导⼊纹理属性Non-Poer of 2 选择None";
[Tooltip("控制背景移动速度"),Range(0,1000)] //不⽀持负值
public float m_speed = 10.0f;
[Tooltip("图⽚从哪个⽅向流动")]
public Direction m_direction;
private GameObject m_preBackground;
private GameObject m_nextBackground;
private Vector2 m_size;              //图⽚长宽
private Vector3 m_position;          //m_preBackground开始位置
private Direction m_trackDirection;  //当direction改变时,主要是⽤于检测这个事件的
private Vector3 m_limit;            //背景移动的边界。超过就回返回;
private Vector3 m_removePosition;    //背景重新移动的位置,超过边界后移动到这⾥。
// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
protected void Start()
{
Init();
}
private void Init()
{
m_direction = m_trackDirection = Direction.LEFT;
m_size = transform.GetComponent<SpriteRenderer>().size / 100;
m_size.x *= transform.localScale.x;
m_size.y *= transform.localScale.y;
m_position = transform.position;
m_preBackground = this.gameObject;
m_preBackground.name = "背景00";
m_nextBackground = new GameObject("背景01");
ansform.position = m_position;
ansform.localScale = ansform.localScale;
m_nextBackground.AddComponent<SpriteRenderer>().sprite = m_preBackground.GetComponent<SpriteRenderer>().sprite;  InitChangeDirection(m_direction);
}
// Update is called every frame, if the MonoBehaviour is enabled.
protected void Update()
{
if (m_trackDirection != m_direction)    //检测是否换⽅向流动。
{
m_trackDirection = m_direction;
InitChangeDirection(m_direction);
}
Flow(m_direction);
}
private void InitChangeDirection(Direction dir) //初始化,当⽅向改变时。
{
transform.position = m_position;
switch (dir)
{
case Direction.UP:
ansform.position = m_position + new Vector3(0, -m_size.y, 0);
m_limit = m_position + new Vector3(0, m_size.y, 0);
break;
case Direction.DOWN:
ansform.position = m_position + new Vector3(0, m_size.y, 0);
m_limit = m_position + new Vector3(0, -m_size.y, 0);
break;
case Direction.LEFT:
ansform.position = m_position + new Vector3(m_size.x, 0, 0);
m_limit = m_position + new Vector3(-m_size.x, 0, 0);
break;
case Direction.RIGHT:
ansform.position = m_position + new Vector3(-m_size.x, 0, 0);
m_limit = m_position + new Vector3(m_size.x, 0, 0);
break;
}
m_removePosition = ansform.position;
}
private void Flow(Direction dir)
{
switch (dir)
{
case Direction.UP:
ansform.position += Vector3.up * Time.deltaTime * m_speed;
ansform.position += Vector3.up * Time.deltaTime * m_speed;
if (ansform.position.y >= m_limit.y)
{
ansform.position = ansform.position - new Vector3(0, m_size.y, 0); //不会产⽣裂缝  }
if (ansform.position.y >= m_limit.y)
{
ansform.position = ansform.position - new Vector3(0, m_size.y, 0); //不会产⽣裂缝  }
break;
case Direction.DOWN:
ansform.position += Vector3.down * Time.deltaTime * m_speed;
ansform.position += Vector3.down * Time.deltaTime * m_speed;
if (ansform.position.y <= m_limit.y)
{
ansform.position = ansform.position + new Vector3(0, m_size.y, 0);
}
if (ansform.position.y <= m_limit.y)
{
ansform.position = ansform.position + new Vector3(0, m_size.y, 0);
}
break;
case Direction.LEFT:
ansform.position += Vector3.left * Time.deltaTime * m_speed;
ansform.position += Vector3.left * Time.deltaTime * m_speed;
if (ansform.position.x <= m_limit.x)
{
ansform.position = ansform.position + new Vector3(m_size.x, 0, 0);
}
单人开发选ue4还是unityif (ansform.position.x <= m_limit.x)
{
ansform.position = ansform.position + new Vector3(m_size.x, 0, 0);
}
break;
case Direction.RIGHT:
ansform.position += Vector3.right * Time.deltaTime * m_speed;
ansform.position += Vector3.right * Time.deltaTime * m_speed;
if (ansform.position.x >= m_limit.x)
{
ansform.position = ansform.position - new Vector3(m_size.x, 0, 0);
}
if (ansform.position.x >= m_limit.x)
{
ansform.position = ansform.position - new Vector3(m_size.x, 0, 0);
}
break;
}
}
}
#region 定义只读Attribute
public class ReadOnlyAttribute : PropertyAttribute
{
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.PropertyField(position, property, label, true);
}
}
#endif
#endregion
效果演⽰

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。