Unity3D培训
美国上市Unity3D培训机构

400-111-8989

VR培训

Android水管工游戏的简单脚本

  • 发布:Unity3D培训
  • 来源:Unity3D教程
  • 时间:2016-01-14 16:55

001

    using UnityEngine;  

002

    using System.Collections;  

003

 

004

    public class TestGoto : MonoBehaviour {  

005

        public GUISkin skin;  

006

        float width;  

007

        float height;  

008

        public Texture up;  

009

        public Texture down;  

010

        public Texture right;  

011

        public Texture left;  

012

        public Texture up_down;  

013

        public Texture right_left;  

014

        int[] images;  

015

        Vector4[] dires;  

016

        int flag;  

017

        int step;  

018

        int coml;  

019

        int row;  

020

        int m;  

021

        int n;  

022

        bool isend;  

023

        bool iswin;  

024

        int[] TF;  

025

        void Start () {  

026

           //width=80*(Screen.width/480);  

027

           width=Screen.width*0.12F;  

028

           height=width;  

029

           flag=0;  

030

           step=0;  

031

           row=4;  

032

           coml=8;  

033

           m=0;  

034

           n=0;  

035

           iswin=false;  

036

           images=new int[32];  

037

           dires=new Vector4[32];  

038

           TF=new int[32];  

039

           for(int k=0;k<=31;k++){  

040

             flag=Random.Range(1,7);    

041

             images[k]=flag;  

042

           }   

043

 

044

           //level 01  

045

        }  

046

 

047

        void OnGUI(){        

048

 

049

           GUI.skin=skin;  

050

           GUI.Window(1,new Rect(0,0,Screen.width,Screen.height),ShowWindow,"");  

051

 

052

           if(isend){  

053

               WinOrLose();   

054

           }  

055

 

056

        }  

057

        void ShowWindow(int windowID){  

058

           if(GUI.Button(new Rect(width*0.2F,0,width,height),"open")){         

059

                 isend=true;          

060

           }  

061

            for(int j=0;j<row;j++){  

062

               for(int i=0;i<coml;i++){              

063

                 if(GUI.Button(new Rect(i*width+width*0.2F,(j+1)*height,width,height),select(images[j*coml+i]))){  

064

                     step+=1;  

065

                     if(images[j*coml+i]<5){  

066

                         images[j*coml+i]+=1;  

067

                         if(images[j*coml+i]==5){  

068

                            images[j*coml+i]=1;  

069

                         }  

070

                      }else{  

071

                         images[j*coml+i]+=1;  

072

                         if(images[j*coml+i]==7){  

073

                            images[j*coml+i]=5;  

074

                         }  

075

                      }  

076

                 }  

077

 

078

                 Vector4 dir;  

079

                 if(select(images[j*coml+i])==up){  

080

                     dir=new Vector4(1,1,0,0);  

081

                 }else if(select(images[j*coml+i])==right){  

082

                     dir=new Vector4(0,1,1,0);  

083

                 }else if(select(images[j*coml+i])==down){  

084

                     dir=new Vector4(0,0,1,1);  

085

                 }else if(select(images[j*coml+i])==left){  

086

                     dir=new Vector4(1,0,0,1);  

087

                 }else if(select(images[j*coml+i])==up_down){  

088

                     dir=new Vector4(1,0,1,0);  

089

                 }else{  

090

                     dir=new Vector4(0,1,0,1);  

091

                 }  

092

                 dires[j*coml+i]=dir;  

093

                 //print(j*coml+i+":"+dires[j*coml+i]);  

094

               }    

095

            }  

096

        }  

097

        Texture select(int n){  

098

            Texture image=new Texture();  

099

            switch(n){  

100

             case 1:  

101

                image=up;  

102

                break;  

103

             case 2:  

104

                image=right;  

105

                break;  

106

             case 3:  

107

                image=down;  

108

                break;  

109

             case 4:  

110

                image=left;  

111

                break;   

112

             case 5:  

113

                image=up_down;  

114

                break;   

115

              case 6:  

116

                image=right_left;  

117

                break;          

118

           }  

119

           return image;  

120

        }  

121

        //判断输赢  

122

        void WinOrLose(){  

123

         switch(images[n*coml+m]){  

124

               //第一种类型的图片,向上+向右  

125

            // if(dires[n*coml+m].x==1&&dires[n*coml+m].y==1){  

126

            case 1:   

127

                 if(n==0){  

128

                    if(m==0){                     

129

                        TF[n*coml+m]=1;   

130

                        m++;  

131

                    }      

132

                 }  //Unity3D教程手册:www.unitymanual.com   

133

                 else{  

134

                    if(m<(coml-1)){  

135

                         if(dires[(n-1)*coml+m].z==1&&dires[n*coml+(m+1)].w==1){  

136

                             TF[n*coml+m]=1;  

137

                             if(TF[(n-1)*coml+m]==1){  

138

                                m++;  

139

                             }  

140

                             if(TF[n*coml+(m+1)]==1){  

141

                                n--;  

142

                             }  

143

                         }  

144

                     }  

145

                 }  

146

 

147

                break;  

148

              //第二种类型的图片 向上+向下  

149

            // if(dires[n*coml+m].x==1&&dires[n*coml+m].z==1){  

150

              case 5:   

151

                  if(n==0){  

152

                     if(m==0){                     

153

                        TF[n*coml+m]=1;   

154

                        n++;  

155

                     }     

156

                  }       

157

                  else if(n>0&&n<(row-1)){  

158

                     if(dires[(n-1)*coml+m].z==1&&dires[(n+1)*coml+m].x==1){  

159

                         TF[n*coml+m]=1;  

160

                         if(TF[(n-1)*coml+m]==1){  

161

                            n++;  

162

                         }  

163

                         if(TF[(n+1)*coml+m]==1){  

164

                            n--;  

165

                         }  

166

                     }                         

167

                  }  

168

                  else{  

169

                      if(m==(coml-1)){  

170

                           print("1:win!!!");  

171

                           iswin=true;  

172

                      }  

173

                  }  //Unity3D教程手册:www.unitymanual.com   

174

                break;  

175

               //第三种类型的图片 向上+向左  

176

            // if(dires[n*coml+m].x==1&&dires[n*coml+m].w==1){  

177

            case 4:  

178

                 if(n>0&&m>0){  

179

                     if(dires[(n-1)*coml+m].z==1&&dires[n*coml+(m-1)].w==1){  

180

                        TF[n*coml+m]=1;  

181

                         if(TF[(n-1)*coml+m]==1){  

182

                            m--;  

183

                         }  

184

                         if(TF[n*coml+(m-1)]==1){  

185

                            n--;  

186

                         }  

187

                     }              

188

                 }  

189

             break;  

190

              //第四种类型的图片 向右+向下  

191

            // if(dires[n*coml+m].y==1&&dires[n*coml+m].z==1){  

192

            case 2:   

193

                 if(n<(row-1)&&m<(coml-1)){  

194

                    if(dires[(n+1)*coml+m].x==1&&dires[n*coml+(m+1)].w==1){  

195

                       TF[n*coml+m]=1;  

196

                       if(TF[(n+1)*coml+m]==1){  

197

                            m++;  

198

                        }  

199

                        if(TF[n*coml+(m+1)]==1){  

200

                            n++;  

201

                        }  

202

                    }  

203

                 }  

204

 

205

              break;  

206

             //第五种类型的图片 向右+向左  

207

            //  if(dires[n*coml+m].y==1&&dires[n*coml+m].w==1){  

208

            case 6:   

209

                 if(m>0&&m<(coml-1)){  

210

                    if(dires[n*coml+(m-1)].y==1&&dires[n*coml+(m+1)].w==1){  

211

                       TF[n*coml+m]=1;  

212

                       if(TF[n*coml+(m-1)]==1){  

213

                            m++;  

214

                        }  

215

                        if(TF[n*coml+(m+1)]==1){  

216

                            m--;  

217

                        }  

218

                    }  

219

                 }  

220

              //Unity3D教程手册:www.unitymanual.com   

221

             break;  

222

             //第六种类型的图片 向下+向左  

223

            //if(dires[n*coml+m].z==1&&dires[n*coml+m].w==1){  

224

            case 3:   

225

                if(n==(row-1)&&m==(coml-1)){  

226

                     print("2:win!!!");  

227

                     iswin=true;  

228

                 }  

229

                 if(m>0&&n<(row-1)){  

230

                    if(dires[n*coml+(m-1)].y==1&&dires[(n+1)*coml+m].x==1){  

231

                       TF[n*coml+m]=1;  

232

                       if(TF[n*coml+(m-1)]==1){  

233

                            n++;  

234

                        }  

235

                        if(TF[(n+1)*coml+m]==1){  

236

                            m--;  

237

                        }  

238

                    }  

239

                 }  

240

 

241

             break;  

242

        }  

243

 

244

         if(iswin){  

245

           print("i win dlnuchunge");  

246

         }  

247

          print("n:"+n+"--m:"+m);  

248

 

249

          for(int a=0;a<32;a++){  

250

            print(a+":"+TF[a]);     

251

         }  

252

      }  

253

 

254

    } 

255

 

预约申请免费试听课

填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:AddComponentMenu 添加组件菜单项
下一篇:Unity3D 3.5:寻路API教程
Unity|超级兔子

Unity|超级兔子

Unity超级角色控制器物理API分析与功能实现

Unity超级角色控制器物理API分析与功能实现

教程:让Unity中物理效果更加逼真

教程:让Unity中物理效果更加逼真

Unity3D 软件下载

Unity3D 软件下载

选择城市和中心
贵州省

广西省

海南省