VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之UGUI_关卡选项界面

1.Image组件—“Source Image”,Set Native Size.

2.Image组件—“Image Type”——Sliced

  编辑要放大缩小的图片,Sprite Editor,采用九宫格切图。

3.创建空物体(作为父物体),添加Componment—Layout—Grid Layout Group(是下面的子物体自动排列)

    创建空物体和创建Panel作为父物体是由区别的!

4.锚点问题,是GUI中容易忽视但却十分重要的点。

5.Prefabs的有效使用,后期通过更改Prefabs来进行完整的映射修改。

6.滚动图片列表的制作

   (1)选项卡图标排列整齐

   (2)创建一个Image组件—“背景图”,命名为ScrollPanel(作为父物体,作为Mask),此时将选项卡托至此背景图下作为子物体。

   (3)在“背景图”上添加“Scroll Rect”滑动组建,Content对象为选项卡图标集合,来控制子物体。

   (4)在“背景图”上添加"Mask"组建。

   (5)按页数进行滑动(分页)脚本

   (6)单选按钮,进入制定页面,并与鼠标滑动脚本相关联。

     

复制代码
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.EventSystems;
 5 using UnityEngine.UI;
 6 
 7 public class LevelButtonScrollRect : MonoBehaviour ,IBeginDragHandler,IEndDragHandler{
 8 
 9     // Use this for initialization
10     private ScrollRect scrollRect;
11     private float[] pageArrayFloats = new[] {0, 0.33333f, 0.66666f, 1}; 
12     void Start ()
13     {
14         scrollRect = GetComponent<ScrollRect>();
15     }
16     
17     // Update is called once per frame
18     void Update () { 
19         
20     }
22     public void OnBeginDrag(PointerEventData eventData)
23     {
24         
25     }
27     public void OnEndDrag(PointerEventData eventData)
28     {
29         //Vector2 offsetTemp = scrollRect.normalizedPosition;
30         float offsetTemp = scrollRect.horizontalNormalizedPosition;
31         print(offsetTemp);    
33     }
34 }
复制代码

 

父物体添加过"ToggleGroup"组件

复制代码
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEditor;
 4 using UnityEngine;
 5 using UnityEngine.EventSystems;
 6 using UnityEngine.UI;
 7 
 8 public class LevelButtonScrollRect : MonoBehaviour ,IBeginDragHandler,IEndDragHandler{
 9 
10     // Use this for initialization
11     private ScrollRect scrollRect;
12     private float[] pageArrayFloats = new[] {0, 0.33333f, 0.66666f, 1};
13     private float targetHorizontalPosition = 0;                              //默认为第一页
14     private bool isDrag = false;
15     public float speed = 5f;
16     void Start ()
17     {
18         scrollRect = GetComponent<ScrollRect>();                             //寻找组件,物体间的通信
19     }
20     
21     // Update is called once per frame
22     void Update () {
23         if (isDrag==false)
24         {
25             scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
26                 targetHorizontalPosition, speed * Time.deltaTime);
27         }
28     }
29 
30     public void OnBeginDrag(PointerEventData eventData)
31     {
32         isDrag = true;
33     }
34 
35     public void OnEndDrag(PointerEventData eventData)
36     {
37         isDrag = false;
38         float posX = scrollRect.horizontalNormalizedPosition;
39         int index = 0;
40         float offset = Mathf.Abs(pageArrayFloats[index] - posX);
41         for (int i = 0; i < pageArrayFloats.Length; i++)
42         {
43             float offsetTemp = Mathf.Abs(pageArrayFloats[i] - posX);
44             if (offsetTemp < offset)
45             {
46                 index = i;
47                 offset = offsetTemp;
48             }
49         }                                                                   //哪一页的位置与当前页的位置差最小,就定在哪一页上
50         //scrollRect.horizontalNormalizedPosition = pageArrayFloats[index];
51         targetHorizontalPosition = pageArrayFloats[index];
52     }
53 }
54 //注:最后一次鼠标滑动的位置是确定的,让这个位置与每一页的位置(4页的位置)比较,差值最小的,则固定在(4页位置)中的哪一页上 
55 //多次判断语句,用到for循环(特别是for{if(){}})
复制代码


相关教程