之前使用的ScrolView的换页功能会有部分偏差,所以现在更换为直接更换页面模块的数据只操作模块就可以了

之前使用的ScrolView的换页功能会有部分偏差,所以现在更换为直接更换页面模块的数据只操作模块就可以了
[code]csharpcode:

#region 课件页面刷新
	[Tooltip("页面数显示")]
	public Text amount;
	private int pageIndex = 0; private int pageCount ;

	/// <summary>
	/// 页面刷新
	/// </summary>
	public void PageUpdate() {
		int surplus = courseList.Count % singlePageAmount;//整页剩余数量,courseList:模块总数量 singlePageAmount:单页显示最大数量

		//总页数
		pageCount = surplus == 0 ? courseList.Count / singlePageAmount : courseList.Count / singlePageAmount + 1;
		////删除最后一页
		//if (pageIndex + 1 > pageCount) 
		//	pageIndex = pageCount - 1;
		int targetIndex = (pageIndex + 1) * singlePageAmount >= courseList.Count ? courseList.Count : (pageIndex + 1) * singlePageAmount;
		//UnityEngine.Debug.Log(targetIndex);
		if (pageCount == pageIndex + 1 && surplus != 0)//当前为最后一页并且最后一页的课件数量不能填满整页
		{
			for (int i = courseParent.childCount - 1; i > surplus - 1; i--)
			{
				Destroy(courseParent.GetChild(i).gameObject);
			}
		}
		else if(pageCount==0&&courseParent.childCount>0)//判断是否删除了最后一个课件
		{
			Destroy(courseParent.GetChild(0).gameObject);
			amount.text = "0/0";//页数需要显示为0
			return;
		}
		if (pageIndex < 0) pageIndex=0; 
		for (int i = pageIndex * singlePageAmount; i < targetIndex; i++)
		{
			if (i-pageIndex* singlePageAmount >= courseParent.childCount)
			{
				GameObject go = Instantiate(courseItem, courseParent);//实例课程
				go.transform.localScale = Vector3.one;
			}
			SetCourseMsg sm = courseParent.GetChild(i - pageIndex * singlePageAmount).GetComponent<SetCourseMsg>();
			sm.Setting(courseList[i]);
			StatusUpdate(sm, courseList[i]);
		}
		amount.text = (pageIndex + 1).ToString() + "/" + pageCount;
	} /// <summary>
	/// 换页按钮事件
	/// </summary>
	public void ChangePage(int nextPage) {
		if (nextPage==1)
		{
			if (pageIndex+1< pageCount)
			{
				pageIndex++;
			}
			else
			{
				UnityEngine.Debug.Log("当前为末尾页");
				return;
			}
		}
		else
		{
			if (pageIndex - 1 >= 0)
			{
				pageIndex--;
			}
			else
			{
				UnityEngine.Debug.Log("当前为初始页");
				return;
			}
		}
		PageUpdate(); 
	}
[code]csharpcode:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
//课件模块操作类,大部分是为了配合服务器命令显示
public class SetCourseMsg : MonoBehaviour {
	public Text courseName;
	public RawImage courseImg;
	public Button deleteBtn;
	private Logic_Course courseControl;
	public Status status;//状态

	private Dictionary<courseStatus, Sprite> statusSprite = new Dictionary<courseStatus, Sprite>();
	// Use this for initialization
	void Awake() {
		courseControl = GetComponentInParent<Logic_Course>();//课程控制脚本
	}
	void Start () {
		Init();
	}
	/// <summary>
	/// 初始化
	/// </summary>
	void Init() {
		if (deleteBtn != null)//删除按键—适用于管理员登录课件显示
		{
			//删除按钮设置删除参数
			deleteBtn.onClick.AddListener(delegate () { courseControl.DeleteCourseBtn_OnClick(courseName); });
		}
		if (courseControl.showType == ShowType.vrview)//状态设置—适用于学员登录
		{
			//状态匹配图片库填充
			statusSprite.Add(courseStatus.train, status._sprite[0]);//已培训
			statusSprite.Add(courseStatus.exam, status._sprite[1]);//已考核
			statusSprite.Add(courseStatus.trainAddExam, status._sprite[2]);//已培训并考核
			statusSprite.Add(courseStatus.forbid, status._sprite[3]);//禁止体验
			//增加课件点击开启事件
			Button btn = GetComponent<Button>();
			btn.onClick.AddListener(delegate() {
				courseControl.StartBtn_OnClick(courseName);
			});
		}

	}
	// Update is called once per frame
	void Update () {
		
	}
	/// <summary>
	/// 设置课程名及图片
	/// </summary>
	public void Setting(string s) {
		courseName.text = s;
		courseImg.texture = Logic_Course.coursePicture.ContainsKey(s) ? Logic_Course.coursePicture[s] : Logic_Course.coursePicture["empty"];
		//SetStatus();
	}

	/// <summary>
	/// 设置显示状态
	/// </summary>
	public void SetStatus() {
		if (courseControl.showType == ShowType.admin) return;

		status._status = Logic_Course.cStatus[courseName.text];
		if (status._status==courseStatus.none)
		{
			status._img.gameObject.SetActive(false);
		}
		else
		{
			status._img.gameObject.SetActive(true);
			status._img.sprite = statusSprite[status._status];
		}
	}
}

/// <summary>
/// 课件操作状态
/// </summary>
[System.Serializable]
public class Status {
	public courseStatus _status;
	public Image _img;
	public Sprite[] _sprite;
}
 

		}
		amount.text = (pageIndex + 1).ToString() + "/" + pageCount;
	}
	
	/// <summary>
	/// 换页按钮事件
	/// </summary>
	public void ChangePage(int nextPage) {
		if (nextPage==1)
		{
			if (pageIndex+1< pageCount)
			{
				pageIndex++;
			}
			else
			{
				UnityEngine.Debug.Log("当前为末尾页");
				return;
			}
		}
		else
		{
			if (pageIndex - 1 >= 0)
			{
				pageIndex--;
			}
			else
			{
				UnityEngine.Debug.Log("当前为初始页");
				return;
			}
		}
		PageUpdate(); 
	}
	#endregion

  

posted on 2021-10-09 19:07  陌洛  阅读(47)  评论(0编辑  收藏  举报