关于ScrollView02

来源:互联网 发布:centos查看ssh端口 编辑:程序博客网 时间:2024/06/07 01:23
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using WhtGame;
using System.IO;
using System;
using System.Collections.Generic;

public class UINews : UIBaseWindow
{

 public RawImage pic;
 public Text title;
 public Text content;
 public Text dateTime;
    private Vector2 contentSize;
    public GameObject scrollView;
    public GameObject contents;
    public Scrollbar ScroVertical;
 // HideInInspector]
 // public NewsRoot obj;

    private ScrollRect newScrollView;
 private int WorkId;
 private bool saveFile;

 [HideInInspector]
 public GlobalControl glb_Control;


 void Awake ()
 {  
        contentSize = content.gameObject.GetComponent<RectTransform>().sizeDelta;
       // newScrollView.vertical = false;
  BtnOnClick ("UINews_MsgBtn", this.OnClick);
  WorkId = 0;
      
//  obj = new NewsRoot ();
  //glb_Control = GameObject.Find ("UIManager").GetComponent<GlobalControl> ();
 }


 private void OnClick (GameObject go)
 {

  if (go.name == "UINews_MsgBtn") {
   OnUIHide ();
  }
 }


 public void SetImg (string thumb)
 {

  StartCoroutine (HTTPClient.GetInstance ().LoadTexture (thumb, RectTexture));
  Debug.Log (pic.texture.name);
 }
 //赋值图片
 public void RectTexture (Texture2D tex)
 {
  if (tex != null) {
   pic.texture = tex;
  }
 }


    private void Dynamic(string intro)
    {
        int rows = intro.Length / 23;
        int redundant = intro.Length % 23;
        if (redundant==0)
        {
            content.gameObject.GetComponent<RectTransform>().sizeDelta=new Vector2(contentSize.x,(rows)*43);
        }
        else
        {
              content.gameObject.GetComponent<RectTransform>().sizeDelta=new Vector2(contentSize.x,(rows+1)*43);
        }
        newScrollView=scrollView.GetComponent<ScrollRect>();
        float height = 0;
        RectTransform[] rect=contents.transform.GetComponentsInChildren<RectTransform>();
        foreach (RectTransform item in rect)
        {
            height += item.rect.height;
            Debug.Log(height);
        }
        //contents的子物体包含了他自己
        height = height - 4800;
        Debug.Log(height);
        if (height <= 1450)
        {
            newScrollView.vertical = false;
        }
        else
        {
            newScrollView.vertical = true;
        }
    }
 public void SetInfo (string img, string tit, string intro, string datetime)
 {
       
        //Debug.Log(content.text.Length);
        //Debug.Log(intro.Length);
  SetImg (img);
  title.text = tit;
  content.text = intro;
        dateTime.text = datetime;
        Dynamic(intro);
        #region
        //int rows= intro.Length / 23;
        //int redundant=intro.Length%23;
        //if (redundant == 0)
        //{
        //    content.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(contentSize.x, (rows) * 43);

        //}
        //else
        //{
        //    content.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(contentSize.x, (rows + 1) * 43);

        //}
        //newScrollView = scrollView.GetComponent<ScrollRect>();
        //float height=0;
        //RectTransform[] rect = contents.transform.GetComponentsInChildren<RectTransform>();
     
        //foreach (RectTransform item in rect)
        //{
        //    height += item.rect.height;
        //    Debug.Log(height);
        //}
        ////contents的子物体包含了他自己
        //  height=height - 4800;
        //  Debug.Log(height);
        //if (height <= 1450)
        //{
        //    newScrollView.vertical = false;
        //}
        //else
        //{
        //    newScrollView.vertical = true;
        //}
        #endregion
    }

 public override void OnUIShow ()
 {
  //SetInfo ();
 }


 public override void OnUIHide ()
 {

  UIManager.GetInstance ().HideWindow (WindowID.WindowID_UINews);
  UIManager.GetInstance ().ShowWindow (WindowID.WindowID_Home);
  UIManager.GetInstance ().ShowWindow (WindowID.WindowID_UIMainMenu);
   
 }
    void Update() {

        if ( newScrollView.vertical ==false)
        {
            ScroVertical.value = 1;
        }
    }
}
0 0