1. 程式人生 > >Unity_給滾動檢視動態新增子物體_066

Unity_給滾動檢視動態新增子物體_066

ScrollView 的父子層次如下:
這裡寫圖片描述
在ScrollView中新增的元件如下
這裡寫圖片描述
在ViewPort中新增的元件如下:
這裡寫圖片描述
在Content中新增的元件如下:
這裡寫圖片描述

在ScrollView 中繫結的指令碼如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class ScrollViewTest : MonoBehaviour,IBeginDragHandler,IEndDragHandler {

    private
ScrollRect scrollRect; private float[] rateArr; //獲取Content的RectTransform private RectTransform contentTransform; //設定新增的預製體 public RectTransform itemTransform; // Use this for initialization void Start () { //獲取自身的ScrollRect元件 scrollRect = GetComponent<ScrollRect>(); contentTransform = transform.Find("ViewPort"
).Find("Content").GetComponent<RectTransform>(); } // Update is called once per frame void Update () { //按A鍵新增子物體 if (Input.GetKeyDown(KeyCode.A)) { Transform temp = Instantiate(itemTransform).transform; temp.SetParent(contentTransform); temp.localPosition = Vector3.zero; temp.localRotation = Quaternion.identity; temp.localScale = Vector3.one; } } }