c# - 在预制实例上存储自定义数据

c# - 在预制实例上存储自定义数据,第1张

我有一个脚本可以生成一颗带有行星的恒星。

每个星球都会有与之相关的信息,例如" type" (熔岩,岩石等)。

当鼠标在行星上工作时,我设置了触发器:

ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if(Physics.Raycast(ray, out hit))
    {
        print (hit.collider.name);
    }

我用以下方法实例化行星

GameObject planetClone = Instantiate (planet, pos, Quaternion.identity) as GameObject;

如何附加字符串变量"输入"一个实例预制?

最佳答案:

1 个答案:

答案 0 :(得分:1)

您可以使用所需参数将脚本附加到预制件,并将方法作为示例传递给

ScriptA scriptA = (ScriptA) planetClone.GetComponent<ScriptA>();
scriptA.SetType("Data you want to pass comes here");

将ScriptA附加到预制件上,在实例化预制件后,您可以使用

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.Timer;
import java.util.ArrayList;
import java.awt.event.KeyAdapter;

public class DigDug extends Applet implements MouseMotionListener, MouseListener, KeyListener
{
   private final Color c_background = new Color(32, 73, 150);
   private final Color c_first = new Color(230, 185, 9);
   private final Color c_second = new Color(224, 128, 18);
   private final Color c_third = new Color(232, 61, 14);
   private final Color c_last = new Color(184, 0, 0);
  private final Color c_score = new Color(232, 42, 96);
   public boolean ifEntered = false;
   public boolean isMoving1 = false;
   private int count = 0;
   private int otherCount = 0;
   private Image flower;
   private Image diggy;
   private Image enemies1;
   private Image enemies2;
   private Image enemies3;
   private Image arrowUp;
   private Image arrowDown;
   private Image arrowLeft;
   private Image arrowRight;
   //private ArrayList<Point> enemies = new ArrayList<Point>();
   Font myFont = new Font("SansSerif",Font.BOLD, 16);
   private Timer timer;
   private Point Up;
   private int digx = 685;
   private int digy = 45;
   private int e1x = 76;
   private int e1y = 210;  //i created a variable that would hold the enemies's values
   private int e2x = 465;
   private int e2y = 342;
   private int e3x = 546;
   private int e3y = 127;
   private boolean ifW = false;
   private boolean ifA = false;
   private boolean ifS = false;
   private boolean ifD = false;
   private boolean yaas = false;
   private int arrowX = digx 5;
   private int arrowY = digy 5;

   private ArrayList<Integer> xpts = new ArrayList<Integer>();
   private ArrayList<Integer> ypts = new ArrayList<Integer>();


   public void init()
   {
      diggy = getImage(getDocumentBase(), "Dig_Dug.png");
      flower = getImage(getDocumentBase(), "flower.gif");
      enemies1 = getImage(getDocumentBase(), "Pooka.png");
      enemies2 = getImage(getDocumentBase(), "Fygar.gif");
      enemies3 = getImage(getDocumentBase(), "Pooka.png");
      arrowUp = getImage(getDocumentBase(), "ArrowUp.png");
      arrowDown = getImage(getDocumentBase(), "ArrowDown.png");
      arrowRight = getImage(getDocumentBase(), "Arrow-Point-Right.png");
      arrowLeft = getImage(getDocumentBase(), "ArrowLeft copy.png");
     setBackground(c_background);
      setSize(700,500);
      addMouseMotionListener(this);
      addMouseListener(this);
      setFont(myFont);
      addKeyListener(this);
      Up = new Point(digx, digy);


   }
   public void paint(Graphics g)
   {
      score(g);
      background(g);
      g.setColor(Color.white);
      //g.fillRect(player.x, player.y, 10, 10);
      if(ifEntered)
      {
         if(digx > 330)
            digx--;   
        else if(digy < 247)
            digy  ;
         repaint();
      }
      if(digx == 330 && digy == 247)
      {
         yaas = true;               //checks if the player has reached spot 
                                     //(random variable i created
      }

      if(yaas == true)             //this if statement is where i am having the problem
      {
         //int count = 0;       
            //int count = 0;
            boolean isUp = false;
            for(int i = 0; i < count; i  )
            {           

               if(e1y == 210)                        
               {
                  while(e1y != 120)
                  {
                    e1y--;
                  }
                  isUp = true;

               }
               if(e1y == 120 && isUp == true)
               {
                   while(e1y != 210)
                   {
                     e1y  ;                   
                   }
                   isUp = false;
               }
               count  ;
            }           
       }          



      g.drawImage(diggy, digx, digy, 20, 20, null);
      g.drawImage(flower, 680, 41, 20, 20, null);
      g.drawImage(enemies1, e1x, e1y, 20, 20, null);
     g.drawImage(enemies2, e2x, e2y, 20, 20, null);
      g.drawImage(enemies3, e3x, e3y, 20, 20, null);

      if(digy > e1y && digy < (e1y 20) && digx > e1x && digx < (e1x 20) ||e1y == digy || e1x == digx )
      {
         background(g);
         g.setColor(Color.white);
         g.drawString("GAME OVER", 294, 260);
     //    repaint();        
      }
      if(digy > e2y && digy < (e2y 20) && digx > e2x && digx < (e2x 20)||e2y == digy || e2x == digx )
      {
         background(g);
         g.setColor(Color.white);
         g.drawString("GAME OVER", 294, 260);
       //  repaint(); 
      }
      if(digy > e3y && digy < (e3y 20) && digx > e3x && digx < (e3x 20) || e3y == digy && e3x == digx)
      {
         background(g);
         g.setColor(Color.white);
         g.drawString("GAME OVER", 294, 260);
        // repaint(); 
      }



   }
   public void score(Graphics g)
   {
      g.setColor(c_score);
      g.drawString("1UP", 101, 21);
      g.drawString("HIGH SCORE", 271, 21);
      g.setColor(Color.white);
      g.drawString(" "   count, 118, 35);
      g.drawString(" "   count, 317, 35);
   }
   public void background(Graphics g)
   {
      g.setColor(c_first);
      g.drawRect(0,65, 700, 120);
      g.fillRect(0, 65, 700, 120);
      g.setColor(c_second);
      g.drawRect(0,166, 700, 120);
      g.fillRect(0,166, 700, 120);
      g.setColor(c_third);
      g.drawRect(0, 267, 700, 120);
      g.fillRect(0, 267, 700, 120);
      g.setColor(c_last);
      g.drawRect(0, 388, 700, 120);
      g.fillRect(0, 388, 700, 120);
   }

这个附加的脚本将属于实例化的GameObject,属性将与它相关联!您可以参考unity documentation了解更多信息

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复