unity 初学笔记

3 篇文章 0 订阅

1.添加背景图片

①Hierarchy下新建canvas->新建image, 拖动图片至Assets路径下;

②Project下点击图片,Inspector中设置texture Type 为Sprite (2D and UI);(这一步比较关键,不然无法正常显示背景图。

③Hierarchy下点击image,在Inspector中设置strectch 属性,以及上下左右距离边框距离为0;

2.修改脚本

Canvas->Add Componant 添加脚本如UIScript,双击后至VS中打开,并修改源码如下:

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Experimental.Rendering;
using System.Text;
using System.Xml;
using MyService;

public class UI : MonoBehaviour
{

    Vector2 pos;
    Canvas canvas;
    RectTransform rectTransform;
    static Texture2D m_texure;
    string strLog;
    string strTitle;
    int SW;//Screen Width
    int SH;//Screen Height
    WebService1 ws;
    bool isErr;
    void Start()
    {
        rectTransform = transform as RectTransform;
        canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
        m_texure = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height);
        strTitle = "Mysql Debug Tool V 1.2";
        strLog = "Welcome to use the Mysql Debug Tool!";
        isErr = false;
        ws = new WebService1();
        //initialEvent();not uesd yet
    }

    // Update is called once per frame
    void Update()
    {
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out pos))
        {
            rectTransform.anchoredPosition = pos;
            //GUI.Button(new Rect(0, 0, pos.x, pos.y), $"{pos.x},{pos.y}");
        }
    }
    public class mGUIStyle
    {
        //Texture2D detectTexture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, false);
        public GUIStyle getLabelStyle(bool isErr = false)
        {
            GUIStyle gslbl = new GUIStyle();
            gslbl.normal.background = null; //这是设置背景填充的
            //gslbl.normal.background = Texture2D.whiteTexture; //这是设置背景填充的
            if (isErr)
            {
                gslbl.normal.textColor = Color.red;
            }
            else
            {
                gslbl.normal.textColor = Color.blue;   //设置字体颜色的
            }
            gslbl.fontSize = 40; //当然,这是字体颜色
            gslbl.alignment = TextAnchor.MiddleCenter;

            return gslbl;
        }

    }
    void OnGUI()
    {
        //GUI.DrawTexture(new Rect(0, 0, pos.x, SH - pos.y), m_texure);
        //GUI.Box(new Rect(10, 10, Screen.width - 10, SH / 5), "Mysql Debug Tool V 1.0", gUIStyle.getLabelStyle());
        mGUIStyle gUIStyle = new mGUIStyle();
        SW = UnityEngine.Screen.width;
        SH = UnityEngine.Screen.height;

        GUI.DrawTexture(new Rect(10, 10, SW - 20, SH / 5), m_texure);
        GUI.DrawTexture(new Rect(10, SH / 5 + 20, SW / 2 - 15, SH * 3 / 5 - 40), m_texure);
        GUI.DrawTexture(new Rect(SW / 2 + 5, SH / 5 + 20, SW / 2 - 15, SH * 3 / 5 - 40), m_texure);
        GUI.DrawTexture(new Rect(10, SH * 4 / 5 - 10, SW - 20, SH / 5), m_texure);

        GUI.Label(new Rect(10, 10, SW - 10, SH / 5), strTitle, gUIStyle.getLabelStyle());
        GUI.Label(new Rect(10, SH * 4 / 5 - 10, SW - 10, SH / 5), strLog, gUIStyle.getLabelStyle(isErr));

        if (GUI.Button(new Rect(10, SH / 5 + 20, 100, 40), "Create DB"))
        {
            strLog = "111";

            ExecutionResult er = ws.CreateDB("mytestdb2");
            if (er.Status)
            {
                strLog = "OK";

                isErr = false;
                strLog = er.Message;
            }
            else
            {
                isErr = true;
                strLog = "Error:CreateDB";
            }

        }
    }

    #region Events
    public event Action<string> OnInfoEvent;
    public void Info(string log)
    {
        OnInfoEvent?.Invoke(log);
    }

    public event Action<string> OnErrorEvent;
    public void Error(string log)
    {
        OnErrorEvent?.Invoke("[Error]" + log);
    }

    private void initialEvent()
    {
        //OnErrorEvent += logUserControl1.UpdateLog;
        //OnInfoEvent += logUserControl1.UpdateLog;

        OnErrorEvent += UpdateMsg;
        OnInfoEvent += UpdateMsg;
    }

    private void UpdateMsg(string strMsg)
    {
        //string str= ws.CreateDB("testDB").Anything.ToString();
    }
    #endregion

}

其中 MyService.cs 由wsdl工具执行cmd脚本后自动生成

C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0>wsdl -out:MyService.cs http://10.10.19.168/webService1.asmx

3.最终配合mysql数据库,通过webservice端口,可成功访问。至此,牛刀小试完成,PC端调试界面显示如下。

4.打包安卓APK,并运行,大功告成,注意File->building setting->Player Setting->other setting需要配置网络权限。

 

public setting:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值