unity学习(1)——控件与脚本的入门

学习资料参考视频 1Unity3d ARPG网络游戏编程实践(一): 网络连接 (unity NetWork与socket的对比)(流畅)_哔哩哔哩_bilibili

1.创建项目

2创建sense

3创建button 

4给button添加脚本,要继承MonoBehavior,命名支持中文的

using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;

public class NetWorkScript : MonoBehaviour
{
    private static NetWorkScript instance;
    private static Socket socket;
    private static string ip = "127.0.0.1";
    private static int port = 8083;
    public static NetWorkScript getInstance()
    {
        if (instance == null)
        {
            instance = new NetWorkScript();
            init();
        }
        return instance;
    }
    public static void init()
    {
        try
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(ip, port);
            Debug.Log("success");
        }
        catch
        {
            Debug.Log("faild");
        }
    }
    public static void 照猫画虎()
    {
        Debug.Log("2");
        init();
        Debug.Log("3");
    }
}

5给button添加OnClick,顺序是控件/类/函数依次选择,button本身也是object(控件)

6点击按钮,console中成功输出内容

你可能感兴趣的:(学习,unity)