2021-07-14Unity:通过扫描线种子算法实现区域填充(油漆桶功能)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using Color = UnityEngine.Color;

public class Demo_texture : MonoBehaviour
{
//计算鼠标点击位置 对应的像素位置,一个是image的左下角,一个是图片的右上角
public Transform textureOrigin;
public Transform textureUPEnd;
//存储点击的图片的texture2D getpixel() 使用
private Texture2D clickTexture2D;
//存储鼠标点击位置的像素值
private Color testColor;
//存储计算出来的像素点的位置
private Vector2 colorPos;
//存储图片定位点的屏幕坐标
private Vector3 textureOriginScreenPosition;
private Vector3 textureEndUPScreenPosition;
//图片 (Read/Write Enabled:读/写 启用)
public Image image;
//点击区域的所变的颜色
public Color color = Color.magenta;
public Stack stackX = new Stack();
public Stack stackY = new Stack();

public Color getColor(int x, int y)
{
    //返回坐标 (x,y) 处的像素颜色
    return clickTexture2D.GetPixel(x, y);
}
public void setColor(int x, int y, Color c)
{
    //设置坐标 (x,y) 处的像素颜色
    clickTexture2D.SetPixel(x, y, c);
}
private void Start()
{
    //将 position 从世界空间变换为屏幕空间。
    textureOriginScreenPosition = Camera.main.WorldToScreenPoint(textureOrigin.position);
    textureEndUPScreenPosition = Camera.main.WorldToScreenPoint(textureUPEnd.position);
}
private void Update()
{
    //返回从摄像机通过屏幕点的光线。
    Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
    //用于从射线投射获取信息
    RaycastHit hit;
    if (Input.GetMouseButtonDown(0))
    {
        //发射射线
        if (Physics.Raycast(r, out hit))
        {
            HitColorChooseImage(hit);
        }
    }
}
private void HitColorChooseImage(RaycastHit hit)
{
    //如果射线命中
    if (hit.collider.name == "Image")
    {
        //获取到纹理引用
        clickTexture2D = hit.collider.gameObject.GetComponent<Image>().sprite.texture;
        CaculateVector2();

    }
}
private void CaculateVector2()
{
                    //鼠标当前的像素坐标位置
    colorPos.x = (Input.mousePosition.x - textureOriginScreenPosition.x) 
        / (textureEndUPScreenPosition.x - textureOriginScreenPosition.x) * clickTexture2D.width;
    colorPos.y = (Input.mousePosition.y - textureOriginScreenPosition.y) 
        / (textureEndUPScreenPosition.y - textureOriginScreenPosition.y) * clickTexture2D.height;
    Debug.Log(colorPos.x+"."+colorPos.y);
    //识别底层颜色
    // if (!(getColor((int)colorPos.x, (int)colorPos.y) == Color.white))
    // {
    //     Debug.Log(colorPos.x+","+colorPos.y);
    //     return;
    // }
    //
    stackX.Clear();
    stackY.Clear();
    int y1;
    bool spanLeft, spanRight;
    stackX.Push((int)colorPos.x);
    stackY.Push((int)colorPos.y);
    while (stackX.Count>0 && stackY.Count>0)
    {
        colorPos.x = stackX.Pop();
        colorPos.y = stackY.Pop();
        y1 = (int)colorPos.y;
        while (y1 >= 0 && getColor((int)colorPos.x, y1) == Color.white)
        {
            y1--;
        }
        y1++;
        spanLeft = false;
        spanRight = false;
        while (y1 < clickTexture2D.height && getColor((int) colorPos.x, y1) == Color.white)
        {
            setColor((int)colorPos.x, y1, color);
            if (!spanLeft && colorPos.x > 0 && getColor((int)colorPos.x - 1, y1) == Color.white)
            {
                stackX.Push((int)colorPos.x - 1);
                stackY.Push(y1);
                spanLeft = true;
            }
            else if (spanLeft && colorPos.x > 0 && getColor((int)colorPos.x - 1, y1) != Color.white)
            {
                spanLeft = false;
            }
            if (!spanRight && colorPos.x < clickTexture2D.width - 1 && getColor((int)colorPos.x + 1, y1) == Color.white)
            {
                stackX.Push((int)colorPos.x + 1);
                stackY.Push(y1);
                spanRight = true;
            }
            else if (spanRight && colorPos.x < clickTexture2D.width - 1 && getColor((int)colorPos.x + 1, y1) != Color.white)
            {
                spanRight = false;
            }
            y1++;
        }
    }
    //实际应用任何先前的 SetPixel 和 SetPixels 更改
    clickTexture2D.Apply();
}

}

在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: #提问者没有完整的问题,无法给出全面的回答,请完善问题# 如果您要问的是如何使用Python和Unity进行Socket通信,那么我可以为您提供一些信息。Socket通信是指通过网络连接在不同的计算机上进行数据传输的过程。Python和Unity可以通过Socket协议实现实时的数据传输和通信。 对于Python和Unity之间的Socket通信,Python负责服务器端,Unity负责客户端。Python服务器将数据发送到Unity客户端,Unity客户端接收并解析数据。最常用的Socket协议是TCP(Transmission Control Protocol)协议,它提供了单一的连接,并将数据包使用特殊的数据结构进行编码和解码。 在开始使用Python和Unity进行Socket通信之前,需要安装Python的Socket模块和Unity的网络功能模块。然后,需要编写Python服务器和Unity客户端的代码,实现数据的发送和接收。 Python Socket模块是Python用于网络编程的核心模块。它提供了内置的套接字对象,可用于创建和操作Socket。在此基础上,可以编写一个Python服务器端程序,并绑定到IP地址和端口上。 Unity客户端使用C#,其自带的网络功能模块可以用于发送和接收数据。在Unity中,需要创建网络Socket连接,并进行连接、发送和接收操作。 总的来说,Python和Unity之间的Socket通信需要使用TCP协议,并需要编写Python服务器和Unity客户端的代码,才能实现数据的实时传输和通信。在实际工程中,还需要考虑数据格式、安全性等问题。 ### 回答2: #Python-Unity-Socket通信:Unity (C#)的实现 Python-Unity-Socket通信可以让Python代码与Unity (C#)代码实现良好的互动。通过Socket通信,Python和Unity都可以发送和接收网络数据,实现各种场景和功能实现Python-Unity Socket通信的步骤如下: 1. 在Python中建立Socket服务器(Server),等待Unity连接 2. 在Unity中建立Socket客户端(Client),连接到Python Socket服务器 3. Python和Unity互相发送和接收数据 在Python中,建立Socket服务器的代码如下: ``` import socket host = '127.0.0.1' port = 8888 s = socket.socket() s.bind((host, port)) s.listen(1) conn, addr = s.accept() print("连接地址:" + str(addr)) while True: data = conn.recv(1024).decode() if not data: break print("接收数据:" + data) conn.sendall(data.encode()) conn.close() ``` 在Unity中,建立Socket客户端的代码如下: ``` using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net.Sockets; using System.Net; using System; public class SocketClient : MonoBehaviour { public string host = "127.0.0.1"; public int port = 8888; private TcpClient client; // Use this for initialization void Start () { try { client = new TcpClient(); client.Connect(host, port); } catch (Exception e) { Debug.Log(e); } string message = "Hello Python"; byte[] data = System.Text.Encoding.UTF8.GetBytes(message); NetworkStream stream = client.GetStream(); stream.Write(data, 0, data.Length); data = new byte[1024]; string responseData = string.Empty; int bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes); Debug.Log("收到服务器消息:" + responseData); stream.Close(); client.Close(); } } ``` 以上代码实现Unity向Python发送一条消息,并接收Python回传的消息。 Python和Unity之间还可以通过Socket发送和接收其他类型的数据,如音频、视频等。需要注意的是,Python和Unity发送和接收数据的格式需要保持一致,可以使用Json、Protobuf等数据格式来统一。 ### 回答3: # 应该题目描述不太准确,这里按照理解给出回答 python-unity-socket-communication,是指使用Python语言和Unity游戏引擎之间进行网络Socket通信。 具体实现中,需要在Unity中编写C#代码,通过Socket连接Python服务器,实现网络通信。 在Unity中,首先需要使用Socket创建一个客户端连接到Python服务器,并通过该连接向Python服务器发送请求和接收响应。同时,需要编写代码来解析Python服务器返回的数据,以便Unity游戏引擎正确地使用它们。 在Python服务器中,需要编写Socket服务器程序,以便监听来自Unity客户端的请求,并对其做出响应。在响应Unity客户端请求时,需要将Python语言中的数据格式转换为Unity游戏引擎所需的格式。 总之,Python-Unity-Socket通信是一种常用且强大的网络通信方式,可以帮助Unity游戏开发人员高效地与Python服务器进行通信,以实现更为复杂的游戏功能和应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值